def evolve_model(end_time, double_star, stars): time = 0 | units.yr dt = 0.5 * end_time / 1000. * 2 converter = nbody_system.nbody_to_si(double_star.mass, double_star.semimajor_axis) stars2 = stars.copy() gravity = Hermite(converter) gravity.particles.add_particle(stars) to_stars = gravity.particles.new_channel_to(stars) from_stars = stars.new_channel_to(gravity.particles) gravity2 = Hermite(converter) gravity2.particles.add_particle(stars2) to_stars2 = gravity2.particles.new_channel_to(stars2) from_stars2 = stars2.new_channel_to(gravity2.particles) period = (2 * numpy.pi * (double_star.semimajor_axis * double_star.semimajor_axis * double_star.semimajor_axis / (constants.G * double_star.mass)).sqrt()) print("Period =", period.as_string_in(units.yr)) print("Mass loss timestep =", dt) print("Steps per period: = {:1.2f}".format(period / dt)) a_an = [] | units.au e_an = [] atemp = double_star.semimajor_axis etemp = double_star.eccentricity a = [] | units.au e = [] ome = [] a2 = [] | units.au e2 = [] ome2 = [] m1 = [] | units.MSun m2 = [] | units.MSun t = [] | units.yr while time < end_time: time += dt gravity.evolve_model(time) gravity2.evolve_model(time) to_stars.copy() to_stars2.copy() dmdtloss = mass_loss_rate(stars.mass) dmdtacc = dmdt_acc(dmdtloss) orbital_elements = orbital_elements_from_binary(stars, G=constants.G) orbital_elements2 = orbital_elements_from_binary(stars2, G=constants.G) # etemp = orbital_elements[3] # atemp = orbital_elements[2] dadt = dadt_masschange(atemp, stars.mass, dmdtloss + dmdtacc) dedt = dedt_masschange(etemp, stars.mass, dmdtloss + dmdtacc) dadt += dadt_momentumchange(atemp, etemp, stars.mass, dmdtacc) dedt += dedt_momentumchange(etemp, stars.mass, dmdtacc) h = (constants.G * stars.mass.sum() * atemp * (1 - etemp * etemp))**0.5 dhdt = dhdt_momentumchange(h, stars.mass, dmdtacc) atemp = atemp + dadt * dt etemp = etemp + dedt * dt a_an.append(atemp) e_an.append(etemp) kick_from_accretion(stars, dmdtacc, dt) dhdt_dadt_to_kick(stars2, dhdt, dadt, dmdtloss + dmdtacc, dt) stars.mass += (dmdtloss + dmdtacc) * dt stars2.mass += (dmdtloss + dmdtacc) * dt from_stars.copy() from_stars2.copy() a.append(orbital_elements[2]) e.append(orbital_elements[3]) ome.append(orbital_elements[7]) a2.append(orbital_elements2[2]) e2.append(orbital_elements2[3]) ome2.append(orbital_elements2[7]) m1.append(stars[0].mass) m2.append(stars[1].mass) t.append(time) print("time=", time.in_(units.yr), "a=", a[-1].in_(units.RSun), "e=", e[-1], "m=", stars.mass.in_(units.MSun), "end=\r") gravity.stop() gravity2.stop() from matplotlib import pyplot pyplot.rc('text', usetex=True) pyplot.rcParams.update({'font.size': 16}) fig, axis = pyplot.subplots(nrows=2, ncols=2, sharex=True, figsize=(13, 6)) axis[0][0].plot(t.value_in(units.yr), a.value_in(units.RSun), label="nbody (direct)", lw=2) axis[0][0].plot(t.value_in(units.yr), a2.value_in(units.RSun), label="nbody (heuristic)", lw=2, ls="--") axis[0][0].plot(t.value_in(units.yr), a_an.value_in(units.RSun), label="analytic", lw=2, ls="-.") axis[0][0].set_ylabel("a [$R_\odot$]") axis[0][0].legend() axis[0][1].plot(t.value_in(units.yr), m1.value_in(units.MSun), label="m1", lw=2, c="tab:red") axis[0][1].plot(t.value_in(units.yr), m2.value_in(units.MSun), label="m2", lw=2, c="tab:cyan") axis[0][1].set_ylabel("M [$M_\odot$]") axis[0][1].legend() axis[1][1].plot(t.value_in(units.yr), e, lw=2) axis[1][1].plot(t.value_in(units.yr), e2, lw=2, ls="--") axis[1][1].plot(t.value_in(units.yr), e_an, lw=2, ls="-.") axis[1][1].set_ylabel("e") axis[1][0].plot(t.value_in(units.yr), ome, lw=2, label="nbody (direct)") axis[1][0].plot(t.value_in(units.yr), ome2, lw=2, ls="--", label="nbody (heuristic)") axis[1][0].set_ylabel("$\omega$ [degrees]") axis[1][1].set_xlabel("time [yr]") axis[1][0].set_xlabel("time [yr]") pyplot.tight_layout() pyplot.subplots_adjust(hspace=0, top=0.93, bottom=0.1) pyplot.suptitle("mloss+macc+momentum change, same model") pyplot.savefig("comparisons.png") pyplot.show()
def evolve_model(end_time, double_star, stars): time = 0 | units.yr dt = 0.5 * end_time / 1000. converter = nbody_system.nbody_to_si(double_star.mass, double_star.semimajor_axis) gravity = Hermite(converter) gravity.particles.add_particle(stars) to_stars = gravity.particles.new_channel_to(stars) from_stars = stars.new_channel_to(gravity.particles) a_an = [] | units.au e_an = [] # dt_an = dt/1000. atemp = double_star.semimajor_axis etemp = double_star.eccentricity print(atemp) a = [] | units.au e = [] m = [] | units.MSun t = [] | units.yr while time < end_time: # ana_time = time # ana_time_end = time + dt # while ana_time < ana_time_end: # dmdt_ana = -1*mass_loss_rate(stars.mass) # print(atemp) # dadt = dadt_massloss(atemp, stars.mass, dmdt_ana) # dedt = dedt_massloss(etemp, stars.mass, dmdt_ana) # atemp = dadt*dt # etemp = dedt*dt # ana_time += dt_an time += dt gravity.evolve_model(time) to_stars.copy() dmdt = mass_loss_rate(stars.mass) dadt = dadt_massloss(atemp, stars.mass, dmdt) dedt = dedt_massloss(etemp, stars.mass, dmdt) atemp = atemp + dadt * dt etemp = etemp + dedt * dt a_an.append(atemp) e_an.append(etemp) stars.mass -= dmdt * dt from_stars.copy() orbital_elements = orbital_elements_from_binary(stars, G=constants.G) a.append(orbital_elements[2]) e.append(orbital_elements[3]) m.append(stars.mass.sum()) t.append(time) print("time=", time.in_(units.yr), "a=", a[-1].in_(units.RSun), "e=", e[-1], "m=", stars.mass.in_(units.MSun)) gravity.stop() from matplotlib import pyplot fig, axis = pyplot.subplots(nrows=2, ncols=2, sharex=True) axis[0][0].scatter(t.value_in(units.yr), a.value_in(units.RSun)) axis[0][0].scatter(t.value_in(units.yr), a_an.value_in(units.RSun)) axis[0][0].set_ylabel("a [$R_\odot$]") axis[0][1].scatter(t.value_in(units.yr), m.value_in(units.MSun)) axis[0][1].set_ylabel("M [$M_\odot$]") axis[1][1].scatter(t.value_in(units.yr), e) axis[1][1].scatter(t.value_in(units.yr), e_an) axis[1][1].set_ylabel("e") axis[1][1].set_xlabel("time [yr]") axis[1][0].set_xlabel("time [yr]") pyplot.show() pyplot.savefig("mloss.png")
def kick(self, dt): kinetic_energy_before = self.particles.kinetic_energy() dmdt = mass_loss_rate(self.particles.mass) self.particles.mass -= dmdt*dt kinetic_energy_after = self.particles.kinetic_energy() return kinetic_energy_after - kinetic_energy_before
def evolve_model(end_time, double_star, stars): time = 0 | units.yr dt = 0.5 * end_time / 1000. * 2 converter = nbody_system.nbody_to_si(double_star.mass, double_star.semimajor_axis) gravity = Hermite(converter) gravity.particles.add_particle(stars) to_stars = gravity.particles.new_channel_to(stars) from_stars = stars.new_channel_to(gravity.particles) period = (2 * numpy.pi * (double_star.semimajor_axis * double_star.semimajor_axis * double_star.semimajor_axis / (constants.G * double_star.mass)).sqrt()) print("Period =", period.as_string_in(units.yr)) print("Mass loss timestep =", dt) print("Steps per period: = {:1.2f}".format(period / dt)) E_an = [] | (units.km / units.s)**2 h_an = [] | units.km**2 / units.s Etemp = -double_star.mass * constants.G / (2 * double_star.semimajor_axis) htemp = (double_star.mass * constants.G * double_star.semimajor_axis * (1 - double_star.eccentricity * double_star.eccentricity))**0.5 E_num = [] | (units.km / units.s)**2 h_num = [] | units.km**2 / units.s m1 = [] | units.MSun m2 = [] | units.MSun t = [] | units.yr t_an = [] | units.yr dt_an = period * 2.5 time_an = 0 | units.yr while time < end_time: time += dt gravity.evolve_model(time) to_stars.copy() dmdtloss = mass_loss_rate(stars.mass) dmdtacc = dmdt_acc(dmdtloss) orbital_elements = orbital_elements_from_binary(stars, G=constants.G) h = h_from_stars(stars) E = E_from_stars(stars) if time > time_an: #atemp = orbital_elements[2] #htemp = h E_an.append(Etemp) h_an.append(htemp) t_an.append(time) dEdt = dEdt_momentum_secular(htemp, Etemp, stars.mass, dmdtacc) dhdt = dhdt_momentum_secular(htemp, stars.mass, dmdtacc) Etemp = Etemp + dEdt * dt_an htemp = htemp + dhdt * dt_an E_an.append(Etemp) h_an.append(htemp) t_an.append(time + dt_an) time_an += dt_an kick_from_accretion(stars, dmdtacc, dt) #stars.mass += (dmdtloss + dmdtacc) * dt #stars2.mass += (dmdtloss + dmdtacc) * dt from_stars.copy() h_num.append(h) E_num.append(E) m1.append(stars[0].mass) m2.append(stars[1].mass) t.append(time) print("time=", time.in_(units.yr), "h=", h.in_(units.km**2 / units.s), "E=", E.in_((units.km / units.s)**2), "m=", stars.mass.in_(units.MSun), end="\r") gravity.stop() from matplotlib import pyplot pyplot.rc('text', usetex=True) pyplot.rcParams.update({'font.size': 16}) fig, axis = pyplot.subplots(nrows=2, ncols=2, sharex=True, figsize=(13, 6)) axis[0][0].plot(t.value_in(units.yr), E_num.value_in((units.km / units.s)**2), label="nbody (direct)", lw=2) axis[0][0].plot(t_an.value_in(units.yr), E_an.value_in((units.km / units.s)**2), label="analytic", lw=2, ls="-.") axis[0][0].set_ylabel("E [km/s]") axis[0][0].legend() axis[0][1].plot(t.value_in(units.yr), m1.value_in(units.MSun), label="m1", lw=2, c="tab:red") axis[0][1].plot(t.value_in(units.yr), m2.value_in(units.MSun), label="m2", lw=2, c="tab:cyan") axis[0][1].set_ylabel("M [$M_\odot$]") axis[0][1].legend() axis[1][1].plot(t.value_in(units.yr), h_num.value_in(units.km**2 / units.s), lw=2) axis[1][1].plot(t_an.value_in(units.yr), h_an.value_in(units.km**2 / units.s), lw=2, ls="-.") axis[1][1].set_ylabel("h [km$^2$/s]") #axis[1][0].plot(t.value_in(units.yr), ome, lw=2, label="nbody (direct)") #axis[1][0].set_ylabel("$\omega$ [degrees]") axis[1][1].set_xlabel("time [yr]") axis[1][0].set_xlabel("time [yr]") pyplot.tight_layout() pyplot.subplots_adjust(hspace=0, top=0.93, bottom=0.1) pyplot.suptitle("mloss+macc+momentum change, same model") pyplot.savefig("debug.png") pyplot.show()
def evolve_model(end_time, double_star, stars): time = 0 | units.yr dt = 0.5*end_time/1000. converter = nbody_system.nbody_to_si(double_star.mass, double_star.semimajor_axis) gravity = Hermite(converter) gravity.particles.add_particle(stars) to_stars = gravity.particles.new_channel_to(stars) from_stars = stars.new_channel_to(gravity.particles) period = ( 2*numpy.pi * ( double_star.semimajor_axis*double_star.semimajor_axis*double_star.semimajor_axis / (constants.G*double_star.mass) ).sqrt() ) print("Period =", period.as_string_in(units.yr)) print("Mass loss timestep =", dt) print("Steps per period: = {:1.2f}".format(period/dt)) a_an = [] | units.au e_an = [] atemp = double_star.semimajor_axis etemp = double_star.eccentricity print(atemp) a = [] | units.au e = [] m = [] | units.MSun t = [] | units.yr while time<end_time: time += dt gravity.evolve_model(time) to_stars.copy() dmdt = mass_loss_rate(stars.mass) dadt = dadt_masschange(atemp, stars.mass, dmdt) dedt = dedt_masschange(etemp, stars.mass, dmdt) atemp = atemp + dadt*dt etemp = etemp + dedt*dt a_an.append(atemp) e_an.append(etemp) stars.mass += dmdt * dt from_stars.copy() orbital_elements = orbital_elements_from_binary(stars, G=constants.G) a.append(orbital_elements[2]) e.append(orbital_elements[3]) m.append(stars.mass.sum()) t.append(time) print("time=", time.in_(units.yr), "a=", a[-1].in_(units.RSun), "e=", e[-1], "m=", stars.mass.in_(units.MSun), end="\r") gravity.stop() from matplotlib import pyplot fig, axis = pyplot.subplots(nrows=2, ncols=2, sharex=True) axis[0][0].plot(t.value_in(units.yr), a.value_in(units.RSun), label="nbody") axis[0][0].plot(t.value_in(units.yr), a_an.value_in(units.RSun), label="analytic") axis[0][0].set_ylabel("a [$R_\odot$]") axis[0][0].legend() axis[0][1].plot(t.value_in(units.yr), m.value_in(units.MSun)) axis[0][1].set_ylabel("M [$M_\odot$]") axis[1][1].plot(t.value_in(units.yr), e) axis[1][1].plot(t.value_in(units.yr), e_an) axis[1][1].set_ylabel("e") axis[1][1].set_xlabel("time [yr]") axis[1][0].set_xlabel("time [yr]") pyplot.savefig("mloss.png") pyplot.show()