def genDMGalaxy(n_stars, m_stars, m_bh, DM_mass): # masses = np.array([m_bh] + [m_star]*n_stars) # masses = np.array([m_star]*n_stars) masses = np.insert(m_stars, 0, m_bh) r = np.sort(RadDist.radSample(size=n_stars)) theta = np.random.uniform(0, 2*np.pi, n_stars) x = r*np.cos(theta) y = r*np.sin(theta) z = np.zeros(n_stars) positions = np.column_stack((x, y, z)) r_max = r[-1] norm_const = r_max / sc.RCmw - np.arctan(r_max / sc.RCmw) v_norm = 3*np.sqrt(sc.G*np.cumsum(masses)[:-1]/r + sc.G*DM_mass/r * (r/sc.RCmw - np.arctan(r/sc.RCmw))/norm_const) print('v_avg', np.sum(v_norm)/len(v_norm)) # plt.plot([sc.RCmw, sc.RCmw], [0, 1e2]) plt.plot(r, v_norm) # plt.xlim(0, 2.4e19) # plt.ylim(0, 3e4) # plt.show() v_unit_vec = np.column_stack((-np.sin(theta), np.cos(theta), np.zeros(n_stars))) velocities = v_norm.reshape((n_stars, 1)) * v_unit_vec positions = np.insert(positions, 0, np.zeros(3), 0) # add black hole (already present in masses) momentum = velocities*np.expand_dims(m_stars, axis=1) velocities = np.insert(velocities, 0, np.zeros(3), 0) # -np.sum(momentum, axis=0)/m_bh, 0) return utils.zip_to_bodylist(positions, velocities, masses), r, v_norm
def gen_galaxy(r, theta, v, m, m_bh): n_stars = len(r) masses = np.insert(m, 0, m_bh) x = r * np.cos(theta) y = r * np.sin(theta) z = np.zeros(n_stars) positions = np.column_stack((x, y, z)) velocities = v positions = np.insert(positions, 0, np.zeros(3), 0) velocities = np.insert(velocities, 0, np.zeros(3), 0) return utils.zip_to_bodylist(positions, velocities, masses)
def genDMGalaxy(n_stars, m_star, m_bh, DM_mass): masses = np.array([m_bh] + [m_star]*n_stars) r = np.sort(RadDist.radSample(size=n_stars)) theta = np.random.uniform(0, 2*np.pi, n_stars) x = r*np.cos(theta) y = r*np.sin(theta) z = np.zeros(n_stars) positions = np.column_stack((x, y, z)) v_norm = np.sqrt(sc.G*np.cumsum(masses)[:-1]/np.linalg.norm(positions, axis=1) + np.sqrt(sc.G*DM_mass*a_0)) # skip first cumsum value, since masses includes black hole v_unit_vec = np.column_stack((-np.sin(theta), np.cos(theta), np.zeros(n_stars))) velocities = v_norm.reshape((n_stars, 1)) * v_unit_vec positions = np.insert(positions, 0, np.zeros(3), 0) # add black hole (already present in masses) velocities = np.insert(velocities, 0, np.zeros(3), 0) # print(velocities) # print(velocities*np.expand_dims(masses, axis=0).T) # print(np.sum(velocities*np.expand_dims(masses, axis=0).T, axis=0)) return utils.zip_to_bodylist(positions, velocities, masses)
velocities = [np.zeros(3)] masses = [m_BH] for r in np.arange(1, 30)*25: # add stars in rings around black hole for theta in np.linspace(0, 2*np.pi, int(3*r/25))[:-1]: positions.append(np.array([r*np.sin(theta), r*np.cos(theta), 0])) velocities.append(np.array([np.cos(theta), -np.sin(theta), 0])*np.sqrt(G*m_BH/r)) masses.append(10) positions = np.array(positions) velocities = np.array(velocities) N = len(positions) all_pos = np.concatenate((positions + center1, positions + center2)) all_vel = np.concatenate((velocities + v1, velocities + v2)) all_m = masses + masses total_bodylist = utils.zip_to_bodylist(all_pos, all_vel, all_m) total_bodylist.check_integrity() print("Benchmarking collision of 2 galaxies with parameters: ") print(f"""\ Number of objects: {N} dt: {dt} thetamax: {thetamax} n_steps: {n_steps} G: {G} """) begin = time.time() cs.LeapFrogC(total_bodylist, dt, n_steps, thetamax, G) end = time.time() print(f"Simulation finished after {end-begin} s")
-np.sum(velocities * m_star, axis=0) / m_bh, 0) print("-----") print('\n'.join([str((var, getsizeof(a))) for var, a in locals().items()])) # print(velocities) # print(velocities*np.expand_dims(masses, axis=0).T) # print(np.sum(velocities*np.expand_dims(masses, axis=0).T, axis=0)) return positions, velocities, masses thetamax = 0.7 n_steps = 2001 m_star = sc.Msol # 3.181651515706176e+30 n_star = int(1e5) pos, vel, mass = genStableGalaxy(n_star, m_star * 10, sc.Msgra) galaxy1 = make_body_array(pos, vel, mass) galaxy2 = utils.zip_to_bodylist(pos, vel, mass) del pos, vel, mass # t1 = time() # result = g.LeapFrog(galaxy1, dt=1e12, n_steps=n_steps, G=sc.G) # t2 = time() # print(f"GPU: {t2-t1} s") t1 = time() cs.LeapFrogC(galaxy2, dt=1e12, n_steps=n_steps, G=sc.G, thetamax=0.7) t2 = time() print(f"CPU: {t2-t1} s")
m_BH = 100000 # mass of black hole positions = [np.zeros(3)] velocities = [np.zeros(3)] masses = [m_BH] for r in np.arange(1, 10) * 25: # add stars in rings around black hole for theta in np.linspace(0, 2 * np.pi, int(3 * r / 25))[:-1]: positions.append(np.array([r * np.sin(theta), r * np.cos(theta), 0])) velocities.append( np.array([np.cos(theta), -np.sin(theta), 0]) * np.sqrt(G * m_BH / r)) masses.append(10) N = len(positions) galaxy_bodies = utils.zip_to_bodylist(positions, velocities, masses) galaxy1 = utils.rotate_bodylist(galaxy_bodies, np.pi / 4, np.array([-1, 0, 0]), np.array([0, 1, 0])) galaxy1 = utils.translate_bodylist(galaxy1, center1) galaxy1 = utils.add_velocity_bodylist(galaxy1, v1) galaxy2 = utils.translate_bodylist(galaxy_bodies, center2) galaxy2 = utils.add_velocity_bodylist(galaxy2, v2) total_bodylist = utils.concatenate_bodylists(galaxy1, galaxy2) total_bodylist.check_integrity() #total_bodylist.save("galaxies.bin") results = cs.LeapFrogSaveC(total_bodylist, dt, n_steps, thetamax, G, 1, 0) # for i in range(len(results)): # bl = cs.BodyList3(results[i, :]) # bl.save(f"galaxies{i:3d}.bin")
N = len(positions) positions = np.concatenate((positions + center1, positions + center2)) velocities = np.concatenate((velocities + v1, velocities + v2)) masses = np.array(masses + masses) ''' N = 100 r = 500 mvar = 10 positions = np.array([np.random.normal(size = N), np.random.normal(size = N), np.zeros(N)]).T * r masses = 10 + (np.random.normal(size = N)**2)*mvar total_mass = sum(masses) transform = np.array([[0, 1, 0], [-1, 0, 0], [0, 0, 1]]) radii = np.linalg.norm(positions, axis=1) velocities = np.tensordot(positions, transform, axes=1)/radii[..., np.newaxis] total_bodylist = utils.zip_to_bodylist(positions, velocities, masses) total_bodylist.check_integrity() begin = time.time() results = cs.LeapFrogSaveC(total_bodylist, dt, n_steps, thetamax, G, save_step) end = time.time() print("Simulation finished after", end-begin, "s") s = utils.get_positions(results) plane = render.Plane(np.array([0, 0, 1]), np.array([0, 0, 0]), np.array([1/32, 0, 0]), np.array([0, 1/32, 0])) render.animate(s, masses, plane, 400, 400)
def run_CPU(galaxy): galaxy_b = utils.zip_to_bodylist(*galaxy) t1 = time() cs.LeapFrogC(galaxy_b, dt=1e12, n_steps=n_steps, G=sc.G, thetamax=0.7) t2 = time() return t2-t1