rho_arr = np.array([5e-10, 2e-10]) * u.kg / u.m**3 rho_infarr = np.array([np.inf, 5e19]) * u.m**-3 rho_negarr = np.array([-5e19, 6e19]) * u.m**-3 T_arr = np.array([1e6, 2e6]) * u.K T_nanarr = np.array([1e6, np.nan]) * u.K T_nanarr2 = np.array([np.nan, 2e6]) * u.K T_allnanarr = np.array([np.nan, np.nan]) * u.K T_negarr = np.array([1e6, -5151.0]) * u.K V = 25.2 * u.m / u.s V_arr = np.array([25, 50]) * u.m / u.s V_nanarr = np.array([25, np.nan]) * u.m / u.s V_allnanarr = np.array([np.nan, np.nan]) * u.m / u.s mu = m_p.to(u.u).value class Test_mass_density: r"""Test the mass_density function in parameters.py.""" def test_particleless(self): with pytest.raises(ValueError): mass_density(1 * u.m**-3) def test_wrong_units(self): with pytest.raises(u.UnitTypeError): mass_density(1 * u.J) def test_handle_nparrays(self): """Test for ability to handle numpy array quantities""" assert_can_handle_nparray(mass_density)
from astropy.constants import sigma_T, m_p from astropy.cosmology import z_at_value, Planck18_arXiv_v2 as cosmo from astropy import units as u T_0 = 2.725E6 #uK c = 299792. #km/s c2 = c**2 #(km/s)^2 #Gcm3= 1.597E-25 #(s/km) with masses in M_sol and lengths in Mpc Gcm2 = 4.785E-20 #(Mpc/M_sun) h = cosmo.H(0).value / 100. crit_dens_0 = cosmo.critical_density0.to(u.solMass / u.Mpc**3).value #M_sun/Mpc**3 sigma_T = sigma_T.to(u.Mpc**2).value # [Mpc^2] m_p = m_p.to(u.M_sun).value # [M_sun] #sigma_T_over_mp = sigma_T.to(value/m_p.to(u.M_sun).value # m^2 f_b = cosmo.Ob0 / cosmo.Om0 c = 299792. #km/s ######################################################### # Halo Transformations ######################################################### # -------------------- # Mass Transformations # -------------------- def M_200c_to_R_200c(M_200c, redshift): """calculate R_200c from M_200c at the given redshift see Eq. 1 in Huang et al 1701.04001"""
import numpy as np # import matplotlib # matplotlib.use('agg') import matplotlib.pylab as plt from matplotlib import colors, cm from astropy.io import fits from astropy.constants import k_B, m_p import glob, gc from scipy.ndimage import gaussian_gradient_magnitude as ggm from astropy import units as u kB = k_B.to('keV/K').value m_p = m_p.to('g').value cmaps = { 'temperature': cm.afmhot, 'photon_emissivity': cm.magma, 'rm': cm.jet, 'beta': cm.inferno_r, 'ggm': cm.magma, 'magnetic_field': cm.viridis } dirs = ['beta=inf/', 'beta=200/', 'beta=100/', 'beta=50/'] def get_tfile(dir, snapnum, field='temperature'): if field == 'ggm': tfiles = glob.glob(dir + 'fitsfiles/photon_emissivity/*fits') else: tfiles = glob.glob(dir + 'fitsfiles/%s/*fits' % field)
def Evo_R_V_SNR(e0, mej2, dens, t): """ Self-similar solution for radius and expansion velocity of the forward and reverse shock of the SNR. Valid until the SNR goes radiative. (Appendix A - Gelfand et al. 2009) Parameters ---------- e0 : float Energy of the SN explosion (tipically 1e51 erg) mej2 : float Ejected mass of the SN explosion (gr) dens : float Hidrogen particle density of the ISM (particle/cm**3) t : array-like Time steps (in year) Returns ------- R_snr : array-like Array with the forward shock radius at each time step (Pc) R_rs : array-like Array with the reverse shock radius at each time step (Pc) v_snr : array-like Array with the forward shock expansion velocity at each time step (cm/s) v_rs : array-like Array with the reverse shock expansion velocity at each time step (cm/s) """ rho = dens * (m_p.to('g').value) #density of ISM in gr/cm**3 #Characteristic lengths R_ch = mej2**(1/3) * rho**(-1/3) t_ch = e0**(-1/2) * mej2**(5/6) * rho**(-1/3) / gp.yr_to_sec #Start calculation R_snr = [] R_rs = [] v_snr = [] v_rs = [] for i in range(len(t)): if t[i] <= (0.25*t_ch): R = 1.12 * R_ch * (t[i]/t_ch)**(2/3) rrs = (1/1.19) * R v = 0.75 * (R_ch/t_ch) * (t[i]/t_ch)**(-1/3) / gp.yr_to_sec vrs = (1/2.38) * v R_snr.append(np.copy(R)) v_snr.append(np.copy(v)) R_rs.append(np.copy(rrs)) v_rs.append(np.copy(vrs)) if t[i] > (0.25*t_ch) and t[i] <= (0.52*t_ch): R = 1.12 * R_ch * (t[i]/t_ch)**(2/3) rrs = (1.49-0.16*((t[i]-(0.25*t_ch))/t_ch)-0.46*np.log(t[i]/(0.25*t_ch))) * (R_ch/t_ch)*t[i] v = 0.75 * (R_ch/t_ch) * (t[i]/t_ch)**(-1/3) / gp.yr_to_sec vrs = (0.5+0.16*((t[i]-(0.25*t_ch))/t_ch))*((R_ch/t_ch)) / gp.yr_to_sec R_snr.append(np.copy(R)) v_snr.append(np.copy(v)) R_rs.append(np.copy(rrs)) v_rs.append(np.copy(vrs)) if t[i] > (0.52*t_ch): R = ((1.12*R_ch*(0.52**(2/3)))**(5/2) + (2.026*e0/rho)**(1/2) * (t[i]-(0.52*t_ch)) * gp.yr_to_sec)**(2/5) rrs = (1.49-0.16*((t[i]-(0.25*t_ch))/t_ch)-0.46*np.log(t[i]/(0.25*t_ch))) * (R_ch/t_ch)*t[i] v = (2/5) * (2.026*e0/rho)**(1/2) * R**(-3/2) vrs = (0.5+0.16*((t[i]-(0.25*t_ch))/t_ch))*((R_ch/t_ch)) / gp.yr_to_sec R_snr.append(np.copy(R)) v_snr.append(np.copy(v)) R_rs.append(np.copy(rrs)) v_rs.append(np.copy(vrs)) R_snr = np.array(R_snr) / gp.pc_to_cm R_rs = np.array(R_rs) / gp.pc_to_cm R_rs[R_rs < 0.0] = 0.0 v_snr = np.array(v_snr) v_rs = np.array((v_rs)) v_rs[R_rs <= 0.0] = 0.0 return R_snr, R_rs, v_snr, v_rs
def extract_SNR_and_pulsar_par( snr_file ): #extract parameters of the SNRs from the relative file and compute all the values need for the computation of the PWNe column_name = ['N', 'r', 'theta', 'z', 'nh', 'type', 'age'] data_snr = pd.read_csv(snr_file, skiprows=lambda x: skip(x), delimiter='\t', usecols=[0, 1, 2, 3, 4, 5, 6], header=None, index_col=0, names=column_name) conv = u.year.to('s') data_snr.age = data_snr.age * 1e3 data_snr.loc[data_snr['type'] == 1, 'E_sn'] = 1e51 data_snr.loc[data_snr['type'] == 4, 'E_sn'] = 3e51 data_snr.loc[data_snr['type'] == 2, 'E_sn'] = 1e51 data_snr.loc[data_snr['type'] == 3, 'E_sn'] = 1e51 data_snr.loc[data_snr['type'] == 1, 'M_ej'] = ((1.4 * u.M_sun).to('g')).value data_snr.loc[data_snr['type'] == 4, 'M_ej'] = ((1. * u.M_sun).to('g')).value data_snr.loc[data_snr['type'] == 2, 'M_ej'] = ((8. * u.M_sun).to('g')).value data_snr.loc[data_snr['type'] == 3, 'M_ej'] = ((2. * u.M_sun).to('g')).value data_snr['l_ch'] = data_snr['E_sn']**(3 / 2) * data_snr['M_ej']**( -5 / 6) * (data_snr['nh'] * (m_p.to('g').value))**(1 / 3) data_snr['t_ch'] = data_snr['E_sn']**(-1 / 2) * data_snr['M_ej']**( 5 / 6) * (data_snr['nh'] * (m_p.to('g').value))**(-1 / 3) / conv samples = metropolis_hastings(f_prop, size=len(data_snr)) data_snr['t*'] = samples[:, 0] data_snr['l*'] = samples[:, 1] data_snr['prob'] = f_prop(samples) data_snr['l0'] = (10**(data_snr['l*'])) * data_snr['l_ch'] data_snr['t0'] = (10**(data_snr['t*'])) * data_snr['t_ch'] data_snr['distance'] = distance(data_snr['r'].values, data_snr['theta'].values, data_snr['z'].values) ##RANDOMLY GENERATION OF (UNKNOWN) PWN PARAMETERS data_snr['eta'] = np.random.uniform( 0.01, 0.4, data_snr.shape[0]) #Magnetization of the PWN data_snr['eps'] = np.random.uniform(0.066, 0.66, data_snr.shape[0]) #Confinment factor data_snr['ebreak'] = np.random.uniform( 0.01, 10, data_snr.shape[0] ) * gp.TeV_to_erg #energy break of the injection spectrum (Broken PWL) data_snr['alpha1'] = np.random.uniform( 1.0, 1.7, data_snr.shape[0]) #first index of the injection spectrum (Broken PWL) data_snr['alpha2'] = np.random.uniform( 2.0, 2.7, data_snr.shape[0] ) #second index of the injection spectrum (Broken PWL) #Photon Background Info data_snr['Tfir'] = np.random.uniform( 1, 50, data_snr.shape[0] ) #Temperature of FIR field in K --> NEED TO INSERT A VALUE STARTING FROM GALPROP data_snr['Ufir'] = np.random.uniform( 0.1, 4, data_snr.shape[0] ) * gp.eV_to_erg #density of FIR field in erg/cm^3 --> NEED TO INSERT A VALUE STARTING FROM GALPROP data_snr['Tnir'] = np.random.uniform( 2000, 5000, data_snr.shape[0] ) #Temperature of NIR field in K --> NEED TO INSERT A VALUE STARTING FROM GALPROP data_snr['Unir'] = np.random.uniform( 0.5, 20, data_snr.shape[0] ) * gp.eV_to_erg #density of NIR field in erg/cm^3 --> NEED TO INSERT A VALUE STARTING FROM GALPROP #Initialize the column with the radii of the SNR (forward shock and reverse shock) and the PWN data_snr['R_fs'] = np.zeros(data_snr.shape[0]) data_snr['R_rs'] = np.zeros(data_snr.shape[0]) data_snr['R_pwn'] = np.zeros(data_snr.shape[0]) del data_snr['type'] del data_snr['t*'] del data_snr['l*'] del data_snr['l_ch'] del data_snr['t_ch'] pwn = data_snr['prob'] > 0.2 return data_snr.loc[pwn]