def test_type_energy(disc_particles_all, halo_particles): """Checks the object.""" (mass_s, pos_s, vel_s, mass_g, pos_g, vel_g) = disc_particles_all mass_dm, pos_dm, vel_dm = halo_particles(N_part=100, seed=42) k_s = 0.5 * (vel_s[:, 0] ** 2 + vel_s[:, 1] ** 2 + vel_s[:, 2] ** 2) k_dm = 0.5 * (vel_dm[:, 0] ** 2 + vel_dm[:, 1] ** 2 + vel_dm[:, 2] ** 2) k_g = 0.5 * (vel_g[:, 0] ** 2 + vel_g[:, 1] ** 2 + vel_g[:, 2] ** 2) p_s = utils.potential( x=pos_s[:, 0], y=pos_s[:, 1], z=pos_s[:, 2], m=mass_s ) p_dm = utils.potential( x=pos_dm[:, 0], y=pos_dm[:, 1], z=pos_dm[:, 2], m=mass_dm ) p_g = utils.potential( x=pos_g[:, 0], y=pos_g[:, 1], z=pos_g[:, 2], m=mass_g ) assert isinstance(p_s, (float, np.float64, np.ndarray)) assert isinstance(p_dm, (float, np.float64, np.ndarray)) assert isinstance(p_g, (float, np.float64, np.ndarray)) assert isinstance(k_s, (float, np.float64, np.ndarray)) assert isinstance(k_dm, (float, np.float64, np.ndarray)) assert isinstance(k_g, (float, np.float64, np.ndarray))
def test_stars_and_gas_pot_energy(disc_particles_all): """Test potential energy STAR and GAS.""" (mass_s, pos_s, vel_s, mass_g, pos_g, vel_g) = disc_particles_all p_s = utils.potential( x=pos_s[:, 0], y=pos_s[:, 1], z=pos_s[:, 2], m=mass_s ) p_g = utils.potential( x=pos_g[:, 0], y=pos_g[:, 1], z=pos_g[:, 2], m=mass_g ) assert (p_s > 0).all() assert (p_g > 0).all()
def test_dm_pot_energy(halo_particles): """Test potential energy DM.""" mass_dm, pos_dm, vel_dm = halo_particles(N_part=100, seed=42) p_s = utils.potential( x=pos_dm[:, 0], y=pos_dm[:, 1], z=pos_dm[:, 2], m=mass_dm ) assert (p_s > 0).all()
def test_energy_method_real_galaxy(mock_real_galaxy): """Test energy method with real galaxy.""" gal = mock_real_galaxy E_tot_s, E_tot_dm, E_tot_g = gal.energy k_s = 0.5 * (gal.arr_.vx_s ** 2 + gal.arr_.vy_s ** 2 + gal.arr_.vz_s ** 2) k_dm = 0.5 * ( gal.arr_.vx_dm ** 2 + gal.arr_.vy_dm ** 2 + gal.arr_.vz_dm ** 2 ) k_g = 0.5 * (gal.arr_.vx_g ** 2 + gal.arr_.vy_g ** 2 + gal.arr_.vz_g ** 2) x = np.hstack((gal.arr_.x_s, gal.arr_.x_dm, gal.arr_.x_g)) y = np.hstack((gal.arr_.y_s, gal.arr_.y_dm, gal.arr_.y_g)) z = np.hstack((gal.arr_.z_s, gal.arr_.z_dm, gal.arr_.z_g)) m = np.hstack((gal.arr_.m_s, gal.arr_.m_dm, gal.arr_.m_g)) pot = utils.potential( da.asarray(x, chunks=100), da.asarray(y, chunks=100), da.asarray(z, chunks=100), da.asarray(m, chunks=100), ) num_s = len(gal.arr_.m_s) num = len(gal.arr_.m_s) + len(gal.arr_.m_dm) pot_s = pot[:num_s] pot_dm = pot[num_s:num] pot_g = pot[num:] np.testing.assert_allclose( E_tot_s.value, k_s - pot_s, rtol=1e-3, atol=1e-3 ) np.testing.assert_allclose( E_tot_dm.value, k_dm - pot_dm, rtol=1e-3, atol=1e-3 ) np.testing.assert_allclose( E_tot_g.value, k_g - pot_g, rtol=1e-3, atol=1e-3 )
def test_daskpotential(halo_particles): """Test potential function.""" mass_dm, pos_dm, vel_dm = halo_particles(N_part=100, seed=42) dask_potential = utils.potential(pos_dm[:, 0], pos_dm[:, 1], pos_dm[:, 2], mass_dm) python_potential = conftest.epot(pos_dm[:, 0], pos_dm[:, 1], pos_dm[:, 2], mass_dm) fortran_potential = np.loadtxt(TEST_DATA_PATH / "fpotential_test.dat") np.testing.assert_allclose(dask_potential, python_potential, rtol=1e-5, atol=1e-5) np.testing.assert_allclose(python_potential, fortran_potential, rtol=1e-5, atol=1e-5) np.testing.assert_allclose(dask_potential, fortran_potential, rtol=1e-5, atol=1e-5)
def potential_energy(self): """ Specific potential energy calculation. Calculates the specific potencial energy of dark matter, star and gas particles. Returns ------- gx : `galaxy object` New instanced galaxy specific potencial energy calculated for stars, dark matter and gas particles. Examples -------- This returns the specific potential energy of stars, dark matter and gas particles. >>> import galaxychop as gc >>> galaxy = gc.Galaxy(...) >>> gpot = galaxy.potential_energy() >>> pot_s, pot_dm, pot_g = gpot.pot_s, gpot.pot_dm, gpot.pot_g Note ---- If the potentials are entered when the `galaxy` object is instanced, then, the calculation of `potential_energy` will raise a `ValueError`. """ m_s = self.arr_.m_s x_s = self.arr_.x_s y_s = self.arr_.y_s z_s = self.arr_.z_s m_dm = self.arr_.m_dm x_dm = self.arr_.x_dm y_dm = self.arr_.y_dm z_dm = self.arr_.z_dm m_g = self.arr_.m_g x_g = self.arr_.x_g y_g = self.arr_.y_g z_g = self.arr_.z_g pot_s = self.arr_.pot_s pot_dm = self.arr_.pot_dm pot_g = self.arr_.pot_g pot_s = self.arr_.pot_s pot_dm = self.arr_.pot_dm pot_g = self.arr_.pot_g eps_s = self.arr_.eps_s eps_dm = self.arr_.eps_dm eps_g = self.arr_.eps_g potential = np.concatenate([pot_s, pot_dm, pot_g]) if np.all(potential == 0.0): x = np.hstack((x_s, x_dm, x_g)) y = np.hstack((y_s, y_dm, y_g)) z = np.hstack((z_s, z_dm, z_g)) m = np.hstack((m_s, m_dm, m_g)) eps = np.max([eps_s, eps_dm, eps_g]) pot = utils.potential( da.asarray(x, chunks=100), da.asarray(y, chunks=100), da.asarray(z, chunks=100), da.asarray(m, chunks=100), da.asarray(eps), ) num_s = len(m_s) num = len(m_s) + len(m_dm) pot_s = pot[:num_s] pot_dm = pot[num_s:num] pot_g = pot[num:] new = attr.asdict(self, recurse=False) del new["arr_"] new.update( pot_s=-pot_s * (u.km / u.s) ** 2, pot_dm=-pot_dm * (u.km / u.s) ** 2, pot_g=-pot_g * (u.km / u.s) ** 2, ) return Galaxy(**new) else: raise ValueError("Potentials are already calculated")