def setCosmology(self, cosmodef): if type(cosmodef) is dict: self.cosmo = cosmology.FlatLambdaCDM(**cosmodef) elif isinstance(cosmodef, basestring): self.cosmo = cosmology.FlatLambdaCDM(**eval(cosmodef)) elif isinstance(cosmodef, cosmology.FLRW): self.cosmo = cosmodef elif cosmodef is None: self.cosmo = cosmology.get_current() else: raise ValueError
def deltavir(redshift, cosmo=None): """ The virial overdensity as a function redshift. This is an approximation parameterized by Bryan & Norman 98, ApJ, 495, 80. Good to 1% for omega(z) = 0.1-1, requires either omega = 1 (flat universe) or omega_lambda = 0. This is given by dissipationless spherical top-hat models of gravitational collapse (Peebles 1990 'The Large Scale structure of the Universe', Eke et al. 1996 MNRAS, 282, 263). """ if cosmo is None: cosmo = cosmology.get_current() if cosmo.Ok0 != 0: if cosmo.Ode0 == 0: x = cosmo.Om(redshift) - 1 return 18 * np.pi**2 + 60 * x - 32 * x**2 else: raise ValueError("Can't compute deltavir for a non-flat cosmology " "with a cosmological constant") else: x = cosmo.Om(redshift) - 1 return 18 * np.pi**2 + 82 * x - 39 * x**2
def deltavir(redshift, cosmo=None): """ The virial overdensity as a function redshift. This is an approximation parameterized by Bryan & Norman 98, ApJ, 495, 80. Good to 1% for omega(z) = 0.1-1, requires either omega = 1 (flat universe) or omega_lambda = 0. This is given by dissipationless spherical top-hat models of gravitational collapse (Peebles 1990 'The Large Scale structure of the Universe', Eke et al. 1996 MNRAS, 282, 263). """ if cosmo is None: cosmo = cosmology.get_current() if cosmo.Ok0 != 0: if cosmo.Ode0 == 0: x = cosmo.Om(redshift) - 1 return 18*pi**2 + 60*x - 32*x**2 else: raise ValueError("Can't compute deltavir for a non-flat cosmology " "with a cosmological constant") else: x = cosmo.Om(redshift) - 1 return 18*pi**2 + 82*x - 39*x**2
def redshifted_to(self, z, adjust_flux=False, dist=None, cosmo=None): """Return a new Spectrum object at a new redshift. The current redshift must be defined (self.z cannot be `None`). A factor of (1 + z) / (1 + self.z) is applied to the wavelength. The inverse factor is applied to the flux so that the bolometric flux (e.g., erg/s/cm^2) remains constant. .. note:: Currently this only works for units in ergs. Parameters ---------- z : float Target redshift. adjust_flux : bool, optional If True, the bolometric flux is adjusted by ``F_out = F_in * (D_in / D_out) ** 2``, where ``D_in`` and ``D_out`` are current and target luminosity distances, respectively. ``D_in`` is self.dist. If self.dist is ``None``, the distance is calculated from the current redshift and given cosmology. dist : float, optional Output distance in Mpc. Used to adjust bolometric flux if ``adjust_flux`` is ``True``. Default is ``None`` which means that the distance is calculated from the redshift and the cosmology. cosmo : `~astropy.cosmology.Cosmology` instance, optional The cosmology used to estimate distances if dist is not given. Default is ``None``, which results in using the default cosmology. Returns ------- spec : Spectrum object A new spectrum object at redshift z. """ if self._z is None: raise ValueError('Must set current redshift in order to redshift' ' spectrum') if self._wunit.physical_type == u.m.physical_type: factor = (1. + z) / (1. + self._z) elif self._wunit.physical_type == u.Hz.physical_type: factor = (1. + self._z) / (1. + z) else: raise ValueError('wavelength must be in wavelength or frequency') d = self._wave * factor f = self._flux / factor if self._error is not None: e = self._error / factor else: e = None if adjust_flux: if self._dist is None and self._z == 0.: raise ValueError("When current redshift is 0 and adjust_flux " "is requested, current distance must be " "defined") if dist is None and z == 0.: raise ValueError("When redshift is 0 and adjust_flux " "is requested, dist must be defined") if cosmo is None: cosmo = cosmology.get_current() if self._dist is None: dist_in = cosmo.luminosity_distance(self._z) else: dist_in = self._dist if dist is None: dist = cosmo.luminosity_distance(z) if dist_in <= 0. or dist <= 0.: raise ValueError("Distances must be greater than 0.") # Adjust the flux factor = (dist_in / dist) ** 2 f *= factor if e is not None: e *= factor return Spectrum(d, f, error=e, z=z, dist=dist, meta=self.meta, unit=self._unit, wave_unit=self._wunit)
def find_rvT(M, z, cosmo=None, mu=0.59): """ Find the virial radius, circular velocity and temperature for a dark matter halo at a given mass and redshift. Parameters ---------- mass : array_like, shape N Total halo mass (including dark matter) in solar masses. z : array_like, shape M Redshift. cosmo : cosmology (optional) The cosmology to use. mu : float (optional) The mean molecular weight. The virial temperature is proportional to mu. Returns ------- vir : named tuple of ndarrays with shape (N, M) The virial radius (proper kpc), circular velocity (km/s) and temperature (K). If N or M is 1, that dimension is suppressed. Notes ----- The value of mu depends on the ionization fraction of the gas; mu = 0.59 for a fully ionized primordial gas (the default), mu = 0.61 for a gas with ionized hydrogen but only singly ionized helium, and mu = 1.22 for neutral primordial gas. Examples -------- >>> vir = find_rvT(1e12, 0) >>> print vir.r, vir.v, vir.T 261.195728743 128.338776885 588643.476006 >>> r,v,T = find_rvT(1e12, [0.5, 1, 1.5, 2]) >>> r array([ 198.57846074, 156.44358398, 127.91018732, 107.74327378]) >>> v array([ 147.1888328 , 165.82959995, 183.39536858, 199.82317152]) >>> T array([ 774259.0063081 , 982789.81965216, 1202024.36495813, 1427014.0148491 ]) """ if isiterable(M): M = np.asarray(M) if cosmo is None: cosmo = cosmology.get_current() # convert to cgs M_g = M * Msun crit_dens = cosmo.critical_density(z) if astropy.__version__ >= '0.3': crit_dens = crit_dens.value#to('g/cm^3').value rho_virial = deltavir(z, cosmo=cosmo) * crit_dens # deal with cases where we have two input arrays if isiterable(M) and isiterable(rho_virial): M_g = np.atleast_2d(M_g).T rho_virial = np.atleast_2d(rho_virial) rvir, vcirc, Tvir = _calc_rvT(M_g, rho_virial, mu=mu) return find_rvT_output(r=rvir/kpc, v=vcirc/km, T=Tvir)
def find_rvT(M, z, cosmo=None, mu=0.59): """ Find the virial radius, circular velocity and temperature for a dark matter halo at a given mass and redshift. Parameters ---------- mass : array_like, shape N Total halo mass (including dark matter) in solar masses. z : array_like, shape M Redshift. cosmo : cosmology (optional) The cosmology to use. mu : float (optional) The mean molecular weight. The virial temperature is proportional to mu. Returns ------- vir : named tuple of ndarrays with shape (N, M) The virial radius (proper kpc), circular velocity (km/s) and temperature (K). If N or M is 1, that dimension is suppressed. Notes ----- The value of mu depends on the ionization fraction of the gas; mu = 0.59 for a fully ionized primordial gas (the default), mu = 0.61 for a gas with ionized hydrogen but only singly ionized helium, and mu = 1.22 for neutral primordial gas. Examples -------- >>> vir = find_rvT(1e12, 0) >>> print vir.r, vir.v, vir.T 261.195728743 128.338776885 588643.476006 >>> r,v,T = find_rvT(1e12, [0.5, 1, 1.5, 2]) >>> r array([ 198.57846074, 156.44358398, 127.91018732, 107.74327378]) >>> v array([ 147.1888328 , 165.82959995, 183.39536858, 199.82317152]) >>> T array([ 774259.0063081 , 982789.81965216, 1202024.36495813, 1427014.0148491 ]) """ if isiterable(M): M = np.asarray(M) if cosmo is None: cosmo = cosmology.get_current() # convert to cgs M_g = M * Msun crit_dens = cosmo.critical_density(z) if astropy.__version__ >= '0.3': crit_dens = crit_dens.value #to('g/cm^3').value rho_virial = deltavir(z, cosmo=cosmo) * crit_dens # deal with cases where we have two input arrays if isiterable(M) and isiterable(rho_virial): M_g = np.atleast_2d(M_g).T rho_virial = np.atleast_2d(rho_virial) rvir, vcirc, Tvir = _calc_rvT(M_g, rho_virial, mu=mu) return find_rvT_output(r=rvir / kpc, v=vcirc / km, T=Tvir)