def _calc_planck_mean_opacity(self): """Calculate the Planck-mean total opacity for each grid cell. See, e.g. Mihalas and Mihalas 1984, for details on the averaging process. Returns ------- kappa_planck_mean : astropy.units.Quantity ndarray Planck-mean opacity (shape Nshells) """ kappa_planck_mean = np.zeros(self.nshells) / units.cm for i in xrange(self.nshells): delta_nu = (self.nu_bins[1:] - self.nu_bins[:-1]) T = self.mdl.plasma.t_rad[i] tmp = (blackbody_nu(self.nu_bins[:-1], T) * delta_nu * self.kappa_tot[:, 0]).sum() tmp /= (blackbody_nu(self.nu_bins[:-1], T) * delta_nu).sum() kappa_planck_mean[i] = tmp return kappa_planck_mean.to("1/cm")
def mockobj(nx, temp): #Create a mock 1D spectrum x = np.arange(nx) l = x * 10 + 5000 wave = l * u.AA #u.AA means Angstrom temperature = temp * u.K flux = blackbody_nu(wave, temperature).value return flux
def setup_grid(self, wavelength=656*u.nm): # use HEALPIX to get evenly sized tiles NSIDE = hp.npix2nside(self.ntiles) colat, lon = hp.pix2ang(NSIDE, np.arange(0, self.ntiles)) # co-latitude theta_values = u.Quantity(colat, unit=u.rad) # longitude phi_values = u.Quantity(lon, unit=u.rad) # the following formulae use the Roche approximation and assume # solid body rotation # solve for radius of rotating star at these co-latitudes if self.distortion: radii = self.radius*np.array([newton(surface, 1.01, args=(self.omega, x)) for x in theta_values]) else: radii = self.radius*np.ones(self.ntiles) # and effective gravities geff = np.sqrt((-const.G*self.mass/radii**2 + self.Omega**2 * radii * np.sin(theta_values)**2)**2 + self.Omega**4 * radii**2 * np.sin(theta_values)**2 * np.cos(theta_values)**2) # now make a ntiles sized CartesianRepresentation of positions self.tile_locs = SphericalRepresentation(phi_values, 90*u.deg-theta_values, radii).to_cartesian() # normal to tile is the direction of the derivate of the potential # this is the vector form of geff above # the easiest way to express it is that it differs from (r, theta, phi) # by a small amount in the theta direction epsilon x = radii/self.radius a = 1./x**2 - (8./27.)*self.omega**2 * x * np.sin(theta_values)**2 b = np.sqrt( (-1./x**2 + (8./27)*self.omega**2 * x * np.sin(theta_values)**2)**2 + ((8./27)*self.omega**2 * x * np.sin(theta_values) * np.cos(theta_values))**2 ) epsilon = np.arccos(a/b) self.tile_dirs = UnitSphericalRepresentation(phi_values, 90*u.deg - theta_values - epsilon) self.tile_dirs = self.tile_dirs.to_cartesian() # and ntiles sized arrays of tile properties tile_temperatures = 2000.0 * u.K * (geff / geff.max())**self.beta # fluxes, not accounting for limb darkening self.tile_scales = np.ones(self.ntiles) self.tile_fluxes = blackbody_nu(wavelength, tile_temperatures) # tile areas spher = self.tile_locs.represent_as(SphericalRepresentation) self.tile_areas = spher.distance**2 * hp.nside2pixarea(NSIDE) * u.rad * u.rad omega_vec = CartesianRepresentation( u.Quantity([0.0, 0.0, self.Omega.value], unit=self.Omega.unit) ) # get velocities of tiles self.tile_velocities = cross(omega_vec, self.tile_locs)
def test_monochromatic_inverse_compton(particle_dists): """ test IC monochromatic against khangulyan et al. """ from ..models import InverseCompton, PowerLaw PL = PowerLaw(1 / u.eV, 1 * u.TeV, 3) # compute a blackbody spectrum with 1 eV/cm3 at 30K from astropy.analytic_functions import blackbody_nu Ephbb = np.logspace(-3.5, -1.5, 100) * u.eV lambdabb = Ephbb.to('AA', equivalencies=u.equivalencies.spectral()) T = 30 * u.K w = 1 * u.eV / u.cm**3 bb = (blackbody_nu(lambdabb, T) * 2 * u.sr / c.cgs / Ephbb / hbar).to('1/(cm3 eV)') Ebbmax = Ephbb[np.argmax(Ephbb**2 * bb)] ar = (4 * sigma_sb / c).to('erg/(cm3 K4)') bb *= (w / (ar * T**4)).decompose() eopts = {'Eemax': 10000 * u.GeV, 'Eemin': 10 * u.GeV, 'nEed': 1000} IC_khang = InverseCompton(PL, seed_photon_fields=[['bb', T, w]], **eopts) IC_mono = InverseCompton(PL, seed_photon_fields=[['mono', Ebbmax, w]], **eopts) IC_bb = InverseCompton(PL, seed_photon_fields=[['bb2', Ephbb, bb]], **eopts) IC_bb_ene = InverseCompton( PL, seed_photon_fields=[['bb2', Ephbb, Ephbb**2 * bb]], **eopts) Eph = np.logspace(-1, 1, 3) * u.GeV assert_allclose(IC_khang.sed(Eph).value, IC_mono.sed(Eph).value, rtol=1e-2) assert_allclose(IC_khang.sed(Eph).value, IC_bb.sed(Eph).value, rtol=1e-2) assert_allclose(IC_khang.sed(Eph).value, IC_bb_ene.sed(Eph).value, rtol=1e-2)
def los_to_dustpol(los,Tdust=18,nu0=353,deltas=1.): from astropy.analytic_functions import blackbody_lambda, blackbody_nu Bnu=blackbody_nu(nu0*u.GHz,Tdust*u.K) sigma_353=1.2e-26*u.cm**2 # Planck 2013 results XI. by Planck Collaboration XI (2014) p0=0.2 ds=deltas*c.pc Bperp2=los['magnetic_field_X']**2+los['magnetic_field_Y']**2 B2=Bperp2+los['magnetic_field_Z']**2 cos2phi=(los['magnetic_field_X']**2-los['magnetic_field_Y']**2)/Bperp2 sin2phi=los['magnetic_field_X']*los['magnetic_field_Y']/Bperp2 cosgam2=Bperp2/B2 nH=los['density']/u.cm**3 dtau=(sigma_353*nH*ds).cgs tau=dtau.cumsum() # print nH.sum()*ds.cgs,tau[-1], np.exp(-tau[-1]) I=Bnu*(1.0-p0*(cosgam2-2./3.0))*dtau#*np.exp(-tau) Q=p0*Bnu*cos2phi*cosgam2*dtau#*np.exp(-tau) U=p0*Bnu*sin2phi*cosgam2*dtau#*np.exp(-tau) return I.sum().to('MJy/sr'),Q.sum().to('MJy/sr'),U.sum().to('MJy/sr')
def test_monochromatic_inverse_compton(particle_dists): """ test IC monochromatic against khangulyan et al. """ from ..models import InverseCompton, PowerLaw PL = PowerLaw(1 / u.eV, 1 * u.TeV, 3) # compute a blackbody spectrum with 1 eV/cm3 at 30K from astropy.analytic_functions import blackbody_nu Ephbb = np.logspace(-3.5, -1.5, 100) * u.eV lambdabb = Ephbb.to('AA', equivalencies=u.equivalencies.spectral()) T = 30 * u.K w = 1 * u.eV / u.cm**3 bb = (blackbody_nu(lambdabb, T) * 2 * u.sr / c.cgs / Ephbb / hbar).to('1/(cm3 eV)') Ebbmax = Ephbb[np.argmax(Ephbb**2 * bb)] ar = (4 * sigma_sb / c).to('erg/(cm3 K4)') bb *= (w / (ar * T**4)).decompose() eopts = {'Eemax': 10000 * u.GeV, 'Eemin': 10 * u.GeV, 'nEed': 1000} IC_khang = InverseCompton(PL, seed_photon_fields=[['bb', T, w]], **eopts) IC_mono = InverseCompton(PL, seed_photon_fields=[['mono', Ebbmax, w]], **eopts) IC_bb = InverseCompton(PL, seed_photon_fields=[['bb2', Ephbb, bb]], **eopts) IC_bb_ene = InverseCompton(PL, seed_photon_fields=[['bb2', Ephbb, Ephbb**2 * bb]], **eopts) Eph = np.logspace(-1, 1, 3) * u.GeV assert_allclose(IC_khang.sed(Eph).value, IC_mono.sed(Eph).value, rtol=1e-2) assert_allclose(IC_khang.sed(Eph).value, IC_bb.sed(Eph).value, rtol=1e-2) assert_allclose(IC_khang.sed(Eph).value, IC_bb_ene.sed(Eph).value, rtol=1e-2)
def calc_md_tlt(self, nu = 340.*u.GHz, k340 = 3.37): self.k340 = k340 self.boltz_tlt = np.ones(self.nstars) self.boltz_tlt_uvg = np.ones(self.nstars) self.boltz_tlt_lam = np.ones(self.nstars) self.boltz_ta = np.ones(self.nstars) self.boltz_tvdp = np.ones(self.nstars) for i in range(self.nstars): self.boltz_tlt[i] = blackbody_nu(nu, self.mytable[i]['Tlt']).value self.boltz_tlt_uvg[i] = blackbody_nu(nu, self.mytable[i]['Tlt_uvg']).value self.boltz_tlt_lam[i] = blackbody_nu(nu, self.mytable[i]['Tlt_lam']).value self.boltz_ta[i] = blackbody_nu(nu, self.mytable[i]['Ta']).value self.boltz_tvdp[i] = blackbody_nu(nu, self.mytable[i]['Tvdp']).value self.boltz_20k = np.zeros(self.nstars)+blackbody_nu(nu, 20. * u.K).value # self.mlt, self.mlt_ep, self.mlt_em = self.get_mass(self.boltz_tlt) self.mlt_uvg, self.mlt_uvg_ep, self.mlt_uvg_em = self.get_mass(self.boltz_tlt_uvg) self.mlt_lam, self.mlt_lam_ep, self.mlt_lam_em = self.get_mass(self.boltz_tlt_lam) self.mta, self.mta_ep, self.mta_em = self.get_mass(self.boltz_ta) self.mtvdp, self.mtvdp_ep, self.mtvdp_em = self.get_mass(self.boltz_tvdp) self.m20k, self.m20k_ep, self.m20k_em = self.get_mass(self.boltz_20k)
def bbody(T,nu): """ Blackbody flux for a given temperature and frequency erg / (cm2 Hz s sr) (cgs system) """ return blackbody_nu(nu, T).cgs.value
def create_flux(self): # use astropy analytic function and astropy.units to specify microns and K. # flux is in erg cm-2 s-1 hz-1 sr-1, but will be scaled by normalization. flux = blackbody_nu(self.wave * u.micron, self.temp * u.K) return flux.value
def testBlack_Body(self): astropy = blackbody_nu(90.e9, 100.) / blackbody_nu(30.e9, 100.) pysm = components.black_body(90., 30., 100.) self.assertAlmostEqual(astropy, pysm)
T_dust = 15 * u.K kappa_dust = 0.6 * u.cm**2 / u.g # Assume the Canonical value here dust_to_gas = 100. beamsize = 1 * u.arcsec # Assuming circular here. Drop units to avoid 'beam' units in intensity beamarea = (np.pi * beamsize**2).to(u.sr) # In mJy/bm, but astropy isn't decomposing automatically intensity = (253.54 * 1e-3) * 1e-26 * u.erg / (u.s * u.Hz * u.cm**2) intensity = intensity / beamarea Sigma_gas = (dust_to_gas / kappa_dust) * \ (intensity / blackbody_nu(nu, T_dust)) Sigma_gas = Sigma_gas.to(u.Msun / u.pc**2) # Multiply by physical beamwidth size # In the case of M33, 1" = 4 pc phys_beamsize = (4 * u.pc) phys_beamarea = (np.pi * phys_beamsize**2) dustmass = Sigma_gas * phys_beamarea print "Mass Surface Density: %s" % (Sigma_gas) print "Mass in beam: %s" % (dustmass)
T_dust = 15 * u.K kappa_dust = 0.6 * u.cm**2/u.g # Assume the Canonical value here dust_to_gas = 100. beamsize = 1 * u.arcsec # Assuming circular here. Drop units to avoid 'beam' units in intensity beamarea = (np.pi * beamsize ** 2).to(u.sr) # In mJy/bm, but astropy isn't decomposing automatically intensity = (253.54 * 1e-3) * 1e-26 * u.erg/(u.s * u.Hz * u.cm**2) intensity = intensity / beamarea Sigma_gas = (dust_to_gas / kappa_dust) * \ (intensity / blackbody_nu(nu, T_dust)) Sigma_gas = Sigma_gas.to(u.Msun/u.pc**2) # Multiply by physical beamwidth size # In the case of M33, 1" = 4 pc phys_beamsize = (4 * u.pc) phys_beamarea = (np.pi*phys_beamsize**2) dustmass = Sigma_gas * phys_beamarea print "Mass Surface Density: %s" % (Sigma_gas) print "Mass in beam: %s" % (dustmass)