def __init__(self): self.zs_ = np.linspace(1e-3, 1, 1000) self.zs = xp.asarray(self.zs_) self.dvc_dz_ = (Planck15.differential_comoving_volume(self.zs_).value * 4 * np.pi) self.dvc_dz = xp.asarray(self.dvc_dz_) self.cached_dvc_dz = None
def __init__(self, z_max=2.3): self.z_max = z_max self.zs_ = np.linspace(1e-3, z_max, 1000) self.zs = xp.asarray(self.zs_) self.dvc_dz_ = Planck15.differential_comoving_volume(self.zs_).value * 4 * np.pi self.dvc_dz = xp.asarray(self.dvc_dz_) self.cached_dvc_dz = None
def integrand( z, R0=BNSrate ): # eq 31 in Regimbau 2012 https://journals.aps.org/prd/pdf/10.1103/PhysRevD.86.122001 norm = R0 / rate_density_coalescence(0) R_local = norm * rate_density_coalescence(z) dv_dz = Planck15.differential_comoving_volume(z).to(u.Gpc**3 / u.sr) #dv_dz_interp(z) return 4 * np.pi * u.sr * dv_dz * R_local * u.yr #1/(1+z) factor accounted for in rate_density_coalescence_integrand
def total_four_volume(lamb, analysis_time, max_redshift=2.3): redshifts = np.linspace(0, max_redshift, 1000) psi_of_z = (1 + redshifts)**lamb normalization = 4 * np.pi / 1e9 * analysis_time total_volume = (np.trapz( Planck15.differential_comoving_volume(redshifts).value / (1 + redshifts) * psi_of_z, redshifts, ) * normalization) return total_volume
def integrand(z, R0=BNSrate, z_max=20, sfr=sfrMD): ''' ''' norm = R0 / rate_density(0, z_max, sfr) R_local = norm * rate_density(z, z_max=z_max, sfr=sfr) dv_dz = Planck15.differential_comoving_volume(z).to(u.Gpc**3 / u.sr) #dv_dz_interp(z) return 4 * np.pi * u.sr * dv_dz * R_local * u.yr * 1 / ( 1 + z ) # 4*pi factor: dv/dz is given in per steridians and so we multiply by the whole sky
def test_powerlaw_volume(self): """ Test that the total volume matches the expected value for a trivial case """ model = redshift.PowerLawRedshift() parameters = dict(lamb=1) total_volume = np.trapz( Planck15.differential_comoving_volume(self.zs).value * 4 * np.pi, self.zs, ) self.assertEqual(total_volume, model.normalisation(parameters))
def rate_per_z(z): """ Equals rate as a function of z, multiplied by the differential comoving volume, multiplied by 4pi steradians for full sphere, multiplied by the neutrino-bright fraction, and then divided by (1+z) to account for time dilation which reduces the rate of transients at high redshifts. :param z: Redshift :return: Transient rate in shell at that redshift """ return rate(z) * cosmo.differential_comoving_volume(z) * \ nu_bright_fraction * (4 * np.pi * u.sr) / (1+z)
def comoving_voxel_volume(z, dnu, omega): """ Get comoving voxel volume in Mpc^3 dnu = Channel width in Hz Omega = pixel area in steradian """ if isinstance(z, np.ndarray): if isinstance(omega, np.ndarray): z, omega = np.meshgrid(z, omega) elif isinstance(dnu, np.ndarray): z, dnu = np.meshgrid(z, dnu) elif isinstance(dnu, np.ndarray) and isinstance(omega, np.ndarray): dnu, omega = np.meshgrid(dnu, omega) nu0 = f21 / (z + 1) - dnu / 2.0 nu1 = nu0 + dnu dz = f21 * (1 / nu0 - 1 / nu1) vol = cosmo.differential_comoving_volume(z).value * dz * omega return vol
def multiple_injections(path="/Users/ilyam/Work/COMPASresults/popsynth/Arash/", filename="mergers.txt", dz=0.001, Tobs=1. / 365.25 / 24 / 60, T0=1234567): random.seed() #path="./" input = open(path + filename, 'r') input.readline() input.readline() count = 0 for line in input: m1, m2, z, rate = np.float_(line.strip().split("\t")) distance = cosmo.luminosity_distance(z).value dVcdz = cosmo.differential_comoving_volume(z).value * 4 * np.pi prob_injection = rate * (dVcdz * dz) / 1e9 * (Tobs / (1 + z)) assert (prob_injection < 1) if (random.random() < prob_injection): one_injection(m1, m2, z, distance, T0, Tobs) count += count input.close() print(count) return count
import numpy as np from astropy.cosmology import Planck15 as cosmo redshifts = np.expm1(np.linspace(np.log(1.), np.log(1. + 3.0), 3000)) d_lum = cosmo.luminosity_distance(redshifts).value dcovdz = cosmo.differential_comoving_volume(redshifts).value def mch_bns(): return 1.4 / 2**.2 def dlum_to_z(dl): ''' Get the redshift for a luminosity distance''' return np.interp(dl, d_lum, redshifts) def z_to_dlum(z): ''' Get the redshift for a luminosity distance''' return np.interp(z, redshifts, d_lum) def z_to_dcovdz(z): ''' Get the redshift for a luminosity distance''' return 4 * np.pi * np.interp(z, redshifts, dcovdz) def get_dLdz(z):
else: while Mlist[i] <= M and i < NM-1: i += 1 j = 0 if z < zlist[0]: j = 1 print "Warning - redshift below minimal redshift in function!" elif z > zlist[Nz-1]: j = Nz - 1 print "Warning - redshift above maximal redshift in function!" else: while zlist[j] <= z and j < Nz-1: j += 1 life = obslife[i-1][j-1] * (Mlist[i] - M) * (zlist[j] - z) life += obslife[i][j-1] * (M-Mlist[i-1]) * (zlist[j] - z) life += obslife[i-1][j] * (Mlist[i]-M) * (z - zlist[j-1]) life += obslife[i][j] * (M-Mlist[i-1]) * (z - zlist[j-1]) life /= (Mlist[i] - Mlist[i-1]) * (zlist[j] - zlist[j-1]) return life # Generate a vector of values which will be multiplied with the corresponding EMRI rate in each bin. # These factors represent the differential comoving volumes and observable lifetimes. volume_obslife_factors = list() for k in range(lnM_nbins): for j in range(z_nbins): lnM_eff = lnM_lower + k*dlnM + dlnM/2 # add half of the bin width to get the midpoint z_eff = z_lower + j*dz + dz/2 volume_obslife_factors.append(ObsLifeSpin4(lnM_eff, z_eff) * Planck15.differential_comoving_volume(z_eff).value / 10e9 * 4 * m.pi)
population_path = sys.argv[ 2] ## path to directory of COSMIC populations at different metallicities save_path = sys.argv[3] ## path to directory to save population all_COSMIC_runs = os.listdir(population_path) mets = [float(x) for x in all_COSMIC_runs] Zsun = 0.017 lowZ = Zsun / 200 #lower bound on metallicity highZ = Zsun #upper bound on metallicity sigmaZ = 0.5 #sigma of the lognormal distribution about the mean metallicity ## grid of BBH formation redshifts zf_grid = np.linspace(0, 20, 10000) SFR = rates.sfr_z(zf_grid, mdl='2017') dVdz = cosmo.differential_comoving_volume(zf_grid) zf_weights = SFR * dVdz * (1 + z_f)**-1 zf_picks = np.random.choice(zf_grid, N, replace=True, p=zf_weights / np.sum(zf_weights)) ## sample a metallicity for each sampled formation redshift Z_picks = [] for zf_pick in zf_picks: ### get metallicity weights for all drawn formation redshifts Z_weights = functions.metal_disp_z(zf_pick, np.array(mets), sigmaZ, lowZ, highZ)
if (sample_initC.shape[0] > 1): sample_initC = sample_initC.iloc[:-1] bpp, bcm, initC, kick_info = Evolve.evolve( initialbinarytable=sample_initC, timestep_conditions=timestep_conditions) merger_type = int(bcm['merger_type'].iloc[-1]) bin_state = bcm['bin_state'].iloc[-1] z_f = sample_bpp['redshift'].iloc[-1] d_L = (1 + z_f) * cosmo.comoving_distance(z_f).to( u.cm).value ## luminosity distance, in cm for flux calculation ## "cosmological weight" of the system using comoving volume element dVdz = cosmo.differential_comoving_volume(z_f) p_cosmic = dVdz * (1 + z_f)**-1 ## get ZAMS masses for the binary ZAMS_mass_k1 = bpp['mass_1'].iloc[0] ZAMS_mass_k2 = bpp['mass_2'].iloc[0] ## get final COSMIC merge types for the binary final_k1 = bpp['kstar_1'].iloc[-1] final_k2 = bpp['kstar_2'].iloc[-1] ## count alive BBHs if (final_k1 == 14 and final_k2 == 14 and bin_state == 0): this_BBH = True #----------------------------------------------------------------------------------
def flat_spectrum_skymodel(variance, nside, ref_chan=0, ref_zbin=0, redshifts=None, freqs=None): """ Generate a full-frequency SkyModel of a flat-spectrum (noiselike) EoR signal. The amplitude of this signal is variance * vol(ref_chan), where vol() gives the voxel volume and ref_chan is a chosen reference point. The generated SkyModel has healpix component type. Parameters ---------- variance: float Variance of the signal, in Kelvin^2, at the reference channel. nside: int HEALPix NSIDE parameter. Must be a power of 2. ref_chan: int Frequency channel to set as reference, if using freqs. ref_zbin: int Redshift bin number to use as reference, if using redshifts. redshifts: numpy.ndarray Redshifts at which to generate maps. Optional if freqs is provided. freqs: numpy.ndarray Frequencies in Hz. Overrides redshifts, setting them to be the redshifts of the 21 cm line corresponding with these frequencies. Optional if redshifts is provided. Returns ------- pyradiosky.SkyModel A SkyModel instance corresponding a white noise-like EoR signal. Notes ----- Either redshifts or freqs must be provided. The history string of the returned SkyModel gives the expected amplitude. """ if freqs is not None: redshifts = f21 / freqs - 1 ref_zbin = np.argsort(redshifts).tolist().index(ref_chan) elif redshifts is None: freqs = f21 / (redshifts + 1) else: raise ValueError("Either redshifts or freqs must be set.") npix = 12 * nside**2 nfreqs = freqs.size omega = 4 * np.pi / npix # Make some noise. stokes = np.zeros((4, npix, nfreqs)) stokes[0, :, :] = np.random.normal(0.0, np.sqrt(variance), (npix, nfreqs)) ref_z = redshifts[ref_zbin] redshifts.sort() ref_zbin = np.where(np.isclose(redshifts, ref_z))[0][0] voxvols = np.zeros(nfreqs) for zi in range(nfreqs - 1): dz = redshifts[zi + 1] - redshifts[zi] vol = (Planck15.differential_comoving_volume( redshifts[zi]).to_value("Mpc^3/sr") * dz * omega) voxvols[zi] = vol voxvols[ nfreqs - 1] = vol # Assume the last redshift bin is the same width as the next-to-last. scale = np.sqrt(voxvols / voxvols[ref_zbin]) stokes *= scale stokes = np.swapaxes(stokes, 1, 2) # Put npix in last place again. if not isinstance(freqs, units.Quantity): freqs *= units.Hz # The true power spectrum amplitude is variance * reference voxel volume. pspec_amp = variance * voxvols[ref_zbin] history_string = ( f"Generated flat-spectrum model, with spectral amplitude {pspec_amp:.3f} " ) history_string += r"K$^2$ Mpc$^3$" return SkyModel( freq_array=freqs, hpx_inds=np.arange(npix), spectral_type="full", nside=nside, stokes=stokes * units.K, history=history_string, )
def volume(z_list, ratio_list): return (4 * np.pi * ratio_list * cosmo.differential_comoving_volume(z_list).value / (1 + z_list)).sum()