def core_density_plot(data, out_file=None, show=True, retrieve=False, color='k'): nbody = Bodies() nbody.copy(data) core_density = np.zeros(nbody.m.shape[0]) distances = np.zeros(nbody.m.shape) for i in range(nbody.m.shape[0]): total_mass = np.sum(nbody.m[i]) center = center_of_mass(nbody.r[i], nbody.m[i]) distances[i] = nbody[i].sort_by_radius(center=center) confined_mass = 0 j = 0 while confined_mass <= total_mass * 0.1: confined_mass += nbody.m[i, j] j += 1 core_density[i] = 3 * confined_mass / 4 / np.pi / distances[i, j]**3 plt.semilogy(nbody.t, core_density, color=color) plt.xlabel('Time, s') plt.ylabel('Core density, kg m$^{-3}$') plt.xlim((0, nbody.t[-1])) if out_file is not None: plt.savefig(out_file) if show is True: plt.show() if retrieve is True: return core_density
def half_mass_radius_center_off_plot(data, out_file=None, show=True, retrieve=False, color='k'): nbody = Bodies() nbody.copy(data) half_mass_radius = np.zeros(nbody.m.shape[0]) distances = np.zeros(nbody.m.shape) for i in range(nbody.m.shape[0]): total_mass = np.sum(nbody.m[i]) distances[i] = nbody[i].sort_by_radius() confined_mass = 0 j = 0 while confined_mass <= total_mass * 0.5: confined_mass += nbody.m[i, j] j += 1 half_mass_radius[i] = distances[i, j] plt.plot(nbody.t, half_mass_radius, color=color) plt.xlabel('Time, s') plt.ylabel('Half mass radius, m') plt.xlim((0, nbody.t[-1])) if out_file is not None: plt.savefig(out_file) if show is True: plt.show() if retrieve is True: return half_mass_radius
def potential_energy_plot_other(data, out_file=None, show=True, retrieve=False, color='b'): nbody = Bodies() nbody.copy(data) all_potential_energy = np.zeros(nbody.m.shape[0]) for j in range(nbody.m.shape[1]): if j % 10 == 0: print j for k in range(j): distances = np.sqrt( np.sum(np.power(nbody.r[:, j, :] - nbody.r[:, k, :], 2), axis=1)) all_potential_energy -= constants.G * nbody.m[:, j] * nbody.m[:, k] / distances plt.plot(nbody.t, all_potential_energy, color=color) plt.xlabel('Time, s') plt.ylabel('Potential energy') plt.xlim((0, nbody.t[-1])) if out_file is not None: plt.savefig(out_file) if show is True: plt.show() if retrieve is True: return all_potential_energy
def potential_energy_center_off_plot(data, out_file=None, show=True, retrieve=False, color='b'): nbody = Bodies() nbody.copy(data) all_potential_energy = np.zeros(nbody.m.shape[0]) potential_energy = np.zeros(nbody.m.shape[1]) distances = np.zeros(nbody.m.shape) for i in range(nbody.m.shape[0]): #distances[i] = np.sqrt(np.sum(nbody.r[i, :, :] ** 2, axis=1)) # Turiu zvaigzdziu atstumus iki centro distances[i] = nbody[i].sort_by_radius() confined_mass = 0 for j in range(nbody.m.shape[1]): potential_energy[j] = -constants.G * confined_mass * nbody.m[ i, j] / distances[i, j] confined_mass += nbody.m[i, j] all_potential_energy[i] = np.sum(potential_energy) plt.plot(nbody.t, all_potential_energy, color=color) plt.xlabel('Time') plt.ylabel('Potential energy') plt.xlim((0, nbody.t[-1])) if out_file is not None: plt.savefig(out_file) if show is True: plt.show() if retrieve is True: return all_potential_energy
def potential_energy_center_off_plot(data, out_file=None, show=True, retrieve=False, color='b'): nbody = Bodies() nbody.copy(data) all_potential_energy = np.zeros(nbody.m.shape[0]) potential_energy = np.zeros(nbody.m.shape[1]) distances = np.zeros(nbody.m.shape) for i in range(nbody.m.shape[0]): #distances[i] = np.sqrt(np.sum(nbody.r[i, :, :] ** 2, axis=1)) # Turiu zvaigzdziu atstumus iki centro distances[i] = nbody[i].sort_by_radius() confined_mass = 0 for j in range(nbody.m.shape[1]): potential_energy[j] = -constants.G * confined_mass * nbody.m[i, j] / distances[i, j] confined_mass += nbody.m[i, j] all_potential_energy[i] = np.sum(potential_energy) plt.plot(nbody.t, all_potential_energy, color=color) plt.xlabel('Time') plt.ylabel('Potential energy') plt.xlim((0, nbody.t[-1])) if out_file is not None: plt.savefig(out_file) if show is True: plt.show() if retrieve is True: return all_potential_energy
def total_energy_plot(data, out_file=None, show=True, show_ratio=False, color='0.5'): nbody = Bodies() nbody.copy(data) all_kinetic_energy = kinetic_energy_plot(data, out_file=None, show=False, retrieve=True) all_potential_energy = potential_energy_plot_other(data, out_file=None, show=False, retrieve=True) if show_ratio is True: plt.clf() plt.plot(nbody.t, all_kinetic_energy / all_potential_energy, color=color) else: plt.plot(nbody.t, all_kinetic_energy + all_potential_energy, color=color) plt.ylabel('Energy') plt.xlabel('Time, s') plt.xlim((0, nbody.t[-1])) if out_file is not None: plt.savefig(out_file) if show is True: plt.show()
def center_of_mass_plot(data, out_file=None, show=True, retrieve=False, color='k'): nbody = Bodies() nbody.copy(data) centers_of_mass = np.zeros((nbody.m.shape[0], 3)) for i in range(nbody.m.shape[0]): centers_of_mass[i] = center_of_mass(nbody.r[i], nbody.m[i]) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(centers_of_mass[:, 0], centers_of_mass[:, 1], zs=centers_of_mass[:, 2], c=color, depthshade=False) ax.set_xlabel('x') ax.set_ylabel('y') ax.set_zlabel('z') if out_file is not None: plt.savefig(out_file) if show is True: plt.show() if retrieve is True: return center_of_mass
def mean_distance_plot(data, out_file=None, show=True, retrieve=False, color='k'): nbody = Bodies() nbody.copy(data) mean_distance = np.zeros(nbody.m.shape[0]) #mass = np.sum(nbody[0].m) #rad = np.zeros(nbody.m.shape[0]) for i in range(nbody.m.shape[0]): center = center_of_mass(nbody.r[i], nbody.m[i]) mean_distance[i] = np.mean(np.sqrt( np.sum((nbody.r[i, :, :] - center)**2, axis=1)), axis=0) #rad[i] = analytic_collapse.radius_at_t(nbody.t[i], 1e14, 3./4./np.pi*mass/1e14**3) plt.plot(nbody.t, mean_distance, color=color) #plt.plot(nbody.t, rad, color='b') plt.xlabel('Time, s') plt.ylabel('Mean distance, m') plt.xlim((0, nbody.t[-1])) if out_file is not None: plt.savefig(out_file) if show is True: plt.show() if retrieve is True: return mean_distance
def uniform_distribution(N, R, koeff=1./3.): #koeff nulemia pasiskirstyma. x_stars, y_stars, z_stars = vector_projection(N, np.power(np.random.sample(N), koeff) * R) bodies = Bodies() bodies.r = np.transpose(np.array([x_stars, y_stars, z_stars])) bodies.m = np.ones(N) * constants.SOLAR_MASS bodies.v = np.zeros(bodies.r.shape) M = np.sum(bodies.m) ro = M/(4./3.*np.pi*R**3) print "Expected collapse time : {:e}".format(np.sqrt(3*np.pi/32./constants.G/ro)) return bodies, 'dist{:.3}'.format(koeff)
def plummer(N, r_pl): M_min = 0.08 # min zvaigzdes mase M_max = 100 # max zvaigzdes mase mStars = IMF_salpeter(N, M_min, M_max) * constants.SOLAR_MASS rStars = np.sort(distance_plummer(N, r_pl)) xStars, yStars, zStars = vector_projection(N, rStars) vStars = velocity_kepler(N, mStars, rStars) v_xStars, v_yStars, v_zStars = vector_projection(N, vStars) bodies = Bodies() bodies.r = np.transpose(np.array([xStars, yStars, zStars])) bodies.m = np.array(mStars) bodies.v = np.transpose(np.array([v_xStars, v_yStars, v_zStars])) return bodies, 'plummer'
def mean_distance_center_off_plot(data, out_file=None, show=True, retrieve=False, color='k'): nbody = Bodies() nbody.copy(data) mean_distance = np.zeros(nbody.m.shape[0]) for i in range(nbody.m.shape[0]): mean_distance[i] = np.mean(np.sqrt(np.sum(nbody.r[i, :, :] ** 2, axis=1)), axis=0) plt.plot(nbody.t, mean_distance, color=color) plt.xlabel('Time, s') plt.ylabel('Mean distance, m') plt.xlim((0, nbody.t[-1])) if out_file is not None: plt.savefig(out_file) if show is True: plt.show() if retrieve is True: return mean_distance
def kinetic_energy_plot(data, out_file=None, show=True, retrieve=False, color='r'): nbody = Bodies() nbody.copy(data) all_kinetic_energy = np.zeros(nbody.m.shape[0]) for i in range(nbody.m.shape[0]): all_kinetic_energy[i] = np.sum(np.sum(nbody.v[i] ** 2, axis=1) * nbody.m[i] / 2.) plt.plot(nbody.t, all_kinetic_energy, color=color) plt.xlabel('Time, s') plt.ylabel('Kinetic energy') plt.xlim((0, nbody.t[-1])) if out_file is not None: plt.savefig(out_file) if show is True: plt.show() if retrieve is True: return all_kinetic_energy
def lagrange_radius_plot(data, out_file=None, show=True): nbody = Bodies() nbody.copy(data) lag_radii_10 = np.zeros(nbody.m.shape[0]) lag_radii_25 = np.zeros(nbody.m.shape[0]) lag_radii_50 = np.zeros(nbody.m.shape[0]) lag_radii_80 = np.zeros(nbody.m.shape[0]) distances = np.zeros(nbody.m.shape) for i in range(nbody.m.shape[0]): total_mass = np.sum(nbody.m[i]) center = center_of_mass(nbody.r[i], nbody.m[i]) distances[i] = nbody[i].sort_by_radius(center=center) confined_mass = 0 j = 0 while confined_mass <= total_mass * 0.1: confined_mass += nbody.m[i, j] j += 1 lag_radii_10[i] = distances[i, j] while confined_mass <= total_mass * 0.25: confined_mass += nbody.m[i, j] j += 1 lag_radii_25[i] = distances[i, j] while confined_mass <= total_mass * 0.5: confined_mass += nbody.m[i, j] j += 1 lag_radii_50[i] = distances[i, j] while confined_mass <= total_mass * 0.8: confined_mass += nbody.m[i, j] j += 1 lag_radii_80[i] = distances[i, j] plt.plot(nbody.t, lag_radii_10, color='r', label='10% mass radii') plt.plot(nbody.t, lag_radii_25, color='b', label='25% mass radii') plt.plot(nbody.t, lag_radii_50, color='g', label='50% mass radii') plt.plot(nbody.t, lag_radii_80, color='k', label='80% mass radii') plt.xlabel('Time, s') plt.ylabel('Lagrange radii, m') plt.xlim((0, nbody.t[-1])) plt.legend(frameon=False, loc=0) if out_file is not None: plt.savefig(out_file) if show is True: plt.show()
def getShip(self, i, shipfile): #method to intitialize a ship into the simulation #takes a timestep and a file containing info on the ship and sets up the required properties with open(shipfile, 'r') as f: for line in f: for word in line.split(): self.ships.append(word) k = 0 while self.ships[k] != 'end': if self.ships[k] == 'new': self.names.append(self.ships[k + 1]) self.masses.append(float(self.ships[k + 3])) self.positions.append(float(self.ships[k + 5])) self.sizes.append(float(self.ships[k + 7])) self.colors.append(self.ships[k + 9]) self.moons.append(self.ships[k + 11]) self.ships.append(self.ships[k + 13]) self.surfaces.append(float(self.ships[k + 15])) deltaV = float(self.ships[k + 17]) angle = float(self.ships[k + 19]) goal = self.ships[k + 21] k += 1 self.objects.append( Bodies(self.names[-1], self.masses[-1], self.positions[-1], self.sizes[-1], self.colors[-1], self.moons[-1], self.ships[-1], i, self.surfaces[-1])) self.objects[-1].goal = goal for j in self.objects: if self.objects[-1].moon == j.name: previousLenght = self.distance(j, self.objects[0], i - 1, False) previousDirection = self.direction(j, self.objects[0], i - 1) planetLength = self.distance(j, self.objects[0], i, False) planetDirection = self.direction(j, self.objects[0], i) self.objects[-1].position = np.vstack( (self.objects[-1].position, previousDirection * self.objects[-1].start + previousDirection * previousLenght)) self.objects[-1].position = np.vstack( (self.objects[-1].position, planetDirection * self.objects[-1].start + planetDirection * planetLength)) planetTan = self.normalize( self.direction(self.objects[-1], j, i)) sunVel = math.sqrt(self.G * self.objects[0].mass / self.distance( self.objects[-1], self.objects[0], i, False)) * self.normalize( self.direction(self.objects[-1], self.objects[0], i)) planetVel = planetTan * 460 launchVel = self.setShipVel(self.objects[-1], i, deltaV, True, angle) self.objects[-1].velocity = np.vstack( (self.objects[-1].velocity, sunVel + planetVel + launchVel)) self.objects[-1].acs = np.vstack( (self.objects[-1].acs, self.acceleration(self.objects[-1], i - 1)))
def potential_energy_plot_other(data, out_file=None, show=True, retrieve=False, color='b'): nbody = Bodies() nbody.copy(data) all_potential_energy = np.zeros(nbody.m.shape[0]) for j in range(nbody.m.shape[1]): if j % 10 == 0: print j for k in range(j): distances = np.sqrt(np.sum(np.power(nbody.r[:, j, :] - nbody.r[:, k, :], 2), axis=1)) all_potential_energy -= constants.G*nbody.m[:, j]*nbody.m[:, k]/distances plt.plot(nbody.t, all_potential_energy, color=color) plt.xlabel('Time, s') plt.ylabel('Potential energy') plt.xlim((0, nbody.t[-1])) if out_file is not None: plt.savefig(out_file) if show is True: plt.show() if retrieve is True: return all_potential_energy
def uniform_sphere(N, R): phi = np.random.random((N)) * 2.0 * np.pi costheta = (np.random.random((N)) - 0.5) * 2.0 u = np.random.random((N)) theta = np.arccos(costheta) r = R * u**(1.0/3.0) x_stars = r * np.sin(theta) * np.cos(phi) y_stars = r * np.sin(theta) * np.sin(phi) z_stars = r * np.cos(theta) bodies = Bodies() bodies.r = np.transpose(np.array([x_stars, y_stars, z_stars])) bodies.m = np.ones(N) * constants.SOLAR_MASS bodies.v = np.zeros(bodies.r.shape) M = np.sum(bodies.m) ro = M/(4./3.*np.pi*R**3) print "Expected collapse time : {:e}".format(np.sqrt(3*np.pi/32./constants.G/ro)) return bodies, 'sphere'
def kinetic_energy_plot(data, out_file=None, show=True, retrieve=False, color='r'): nbody = Bodies() nbody.copy(data) all_kinetic_energy = np.zeros(nbody.m.shape[0]) for i in range(nbody.m.shape[0]): all_kinetic_energy[i] = np.sum( np.sum(nbody.v[i]**2, axis=1) * nbody.m[i] / 2.) plt.plot(nbody.t, all_kinetic_energy, color=color) plt.xlabel('Time, s') plt.ylabel('Kinetic energy') plt.xlim((0, nbody.t[-1])) if out_file is not None: plt.savefig(out_file) if show is True: plt.show() if retrieve is True: return all_kinetic_energy
def mean_distance_plot(data, out_file=None, show=True, retrieve=False, color='k'): nbody = Bodies() nbody.copy(data) mean_distance = np.zeros(nbody.m.shape[0]) #mass = np.sum(nbody[0].m) #rad = np.zeros(nbody.m.shape[0]) for i in range(nbody.m.shape[0]): center = center_of_mass(nbody.r[i], nbody.m[i]) mean_distance[i] = np.mean(np.sqrt(np.sum((nbody.r[i, :, :] - center) ** 2, axis=1)), axis=0) #rad[i] = analytic_collapse.radius_at_t(nbody.t[i], 1e14, 3./4./np.pi*mass/1e14**3) plt.plot(nbody.t, mean_distance, color=color) #plt.plot(nbody.t, rad, color='b') plt.xlabel('Time, s') plt.ylabel('Mean distance, m') plt.xlim((0, nbody.t[-1])) if out_file is not None: plt.savefig(out_file) if show is True: plt.show() if retrieve is True: return mean_distance
def mean_distance_center_off_plot(data, out_file=None, show=True, retrieve=False, color='k'): nbody = Bodies() nbody.copy(data) mean_distance = np.zeros(nbody.m.shape[0]) for i in range(nbody.m.shape[0]): mean_distance[i] = np.mean(np.sqrt(np.sum(nbody.r[i, :, :]**2, axis=1)), axis=0) plt.plot(nbody.t, mean_distance, color=color) plt.xlabel('Time, s') plt.ylabel('Mean distance, m') plt.xlim((0, nbody.t[-1])) if out_file is not None: plt.savefig(out_file) if show is True: plt.show() if retrieve is True: return mean_distance
def addRandomCelestial(self, i): #method that takes a timestep and adds a body with random properties to the system at that timestep mass = random.randrange(10**10, 10**17) size = 1000000000 color = 'y' self.objects.append( Bodies('asteroid', mass, 1, size, color, 'no', 'True', i, 100000)) xpos = random.randrange(-1e14, 1e14) ypos = random.randrange(-1e14, 1e14) self.objects[-1].position = np.vstack( (self.objects[-1].position, np.array([xpos, ypos]))) self.objects[-1].position = np.vstack( (self.objects[-1].position, np.array([xpos, ypos]))) xvel = random.randrange(-150000, 150000) yvel = random.randrange(-150000, 150000) self.objects[-1].velocity = np.vstack( (self.objects[-1].velocity, np.array([xvel, yvel]))) self.objects[-1].acs = np.vstack( (self.objects[-1].acs, self.acceleration(self.objects[-1], i - 1)))
plt.clf() plt.plot(nbody.t, all_kinetic_energy / all_potential_energy, color=color) else: plt.plot(nbody.t, all_kinetic_energy + all_potential_energy, color=color) plt.ylabel('Energy') plt.xlabel('Time, s') plt.xlim((0, nbody.t[-1])) if out_file is not None: plt.savefig(out_file) if show is True: plt.show() import analytic_collapse from matplotlib.colors import colorConverter if __name__ == "__main__": bodies = Bodies() bodies.from_pickle(np.load("plummer_N700_T1000_E1e+11_d2e+14.pkl")) bodies.t = np.load('plummer_N700_T1000_E1e+11_d2e+14_time.npy') #print colorConverter.colors #for i in range(len(bodies[0].color)): #print bodies[0].color[i][:3] #print colorConverter.colors[tuple(bodies[0].color[i][:3])] #print colorConverter.colors.get('k') == bodies[0].color[i][:3] #center_of_mass_plot(bodies) #core_density_plot(bodies) #mean_distance_center_off_plot(bodies, color='r', show=False) #mean_distance_plot(bodies) #half_mass_radius_center_off_plot(bodies, color='r', show=False) #half_mass_radius_plot(bodies) #lagrange_radius_plot(bodies)
plt.plot(nbody.t, all_kinetic_energy + all_potential_energy, color=color) plt.ylabel('Energy') plt.xlabel('Time, s') plt.xlim((0, nbody.t[-1])) if out_file is not None: plt.savefig(out_file) if show is True: plt.show() import analytic_collapse from matplotlib.colors import colorConverter if __name__ == "__main__": bodies = Bodies() bodies.from_pickle(np.load("plummer_N700_T1000_E1e+11_d2e+14.pkl")) bodies.t = np.load('plummer_N700_T1000_E1e+11_d2e+14_time.npy') #print colorConverter.colors #for i in range(len(bodies[0].color)): #print bodies[0].color[i][:3] #print colorConverter.colors[tuple(bodies[0].color[i][:3])] #print colorConverter.colors.get('k') == bodies[0].color[i][:3] #center_of_mass_plot(bodies) #core_density_plot(bodies) #mean_distance_center_off_plot(bodies, color='r', show=False) #mean_distance_plot(bodies) #half_mass_radius_center_off_plot(bodies, color='r', show=False) #half_mass_radius_plot(bodies) #lagrange_radius_plot(bodies)
def __init__(self, launchShips, date1, date2, date3, ship1, ship2, ship3, addAsteriods, testAlignement, universe): self.G = 6.67408 * 10**-11 self.launchShips = launchShips self.date1 = date1 self.date2 = date2 self.date3 = date3 self.ship1 = ship1 self.ship1 = ship1 self.ship1 = ship1 self.addAsteriods = addAsteriods self.testAlignement = testAlignement self.objects = [] self.ships = [] self.info = [] self.names = [] self.masses = [] self.positions = [] self.sizes = [] self.colors = [] self.moons = [] self.count = 2 self.reset = None self.switch = None self.arived = None self.flyby = None self.ships = [] self.surfaces = [] self.alignements = [] self.alignement = None self.crashes = [] self.crashed = [] self.asteroidFactor = 0.0001 with open(universe, 'r') as f: for line in f: for word in line.split(): self.info.append(word) k = 0 while self.info[k] != 'end': #reads values from text file if self.info[k] == 'new': self.names.append(self.info[k + 1]) self.masses.append(float(self.info[k + 3])) self.positions.append(float(self.info[k + 5])) self.sizes.append(float(self.info[k + 7])) self.colors.append(self.info[k + 9]) self.moons.append(self.info[k + 11]) self.ships.append(self.info[k + 13]) self.surfaces.append(float(self.info[k + 15])) if self.info[k] == 'details': self.stepLength = int(self.info[k + 2]) self.steps = int(self.info[k + 4]) k += 1 for i in range(0, len( self.names)): #initilisez the objects and placing moons self.objects.append( Bodies(self.names[i], self.masses[i], self.positions[i], self.sizes[i], self.colors[i], self.moons[i], self.ships[i], 0, self.surfaces[i])) if self.objects[i].moon != "no": for j in self.objects: if self.objects[i].moon == j.name: planetLength = self.distance(j, self.objects[0], 0, False) planetDirection = self.direction(j, self.objects[0], 0) self.objects[ i].position = planetDirection * self.objects[ i].start + planetDirection * planetLength for i in self.objects: #initilising velocities i.velocity = np.array([0, 0]) if i == self.objects[0]: self.zeroPoint = np.array([0, 0]) continue else: i.velocity = i.velocity + math.sqrt( self.G * self.objects[0].mass / self.distance( i, self.objects[0], 0, False)) * self.normalize( self.direction(i, self.objects[0], 0)) if i.moon != "no": for j in self.objects: if i == j or i.moon != j.name: continue else: i.velocity = i.velocity + math.sqrt( self.G * j.mass / self.distance(j, i, 0, False) ) * self.normalize(self.direction(i, j, 0)) i.zeroPoint = self.absDirection(i.velocity, 0, 0) self.launch = self.steps