Skip to content

Commit c6880b0

Browse files
committed
tests for emissivity from GalaxyEnsemble
1 parent a8725a1 commit c6880b0

File tree

3 files changed

+49
-62
lines changed

3 files changed

+49
-62
lines changed

.coveragerc

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
branch = False
44

55
[report]
6+
precision = 2
7+
68
# Regexes for lines to exclude from consideration
79
exclude_lines =
810
# Have to re-enable the standard pragma

ares/populations/GalaxyEnsemble.py

+25-58
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def __dict__(self, name):
7676
def tab_z(self):
7777
if not hasattr(self, '_tab_z'):
7878
h = self._gen_halo_histories()
79+
self._tab_z = h['z']
7980
return self._tab_z
8081

8182
@tab_z.setter
@@ -89,16 +90,6 @@ def tab_t(self):
8990
self._tab_t = self.cosm.t_of_z(self.tab_z) / s_per_yr
9091
return self._tab_t
9192

92-
@property
93-
def tab_dz(self):
94-
if not hasattr(self, '_tab_dz'):
95-
dz = np.diff(self.tab_z)
96-
if np.allclose(np.diff(dz), 0):
97-
dz = dz[0]
98-
self._tab_dz = dz
99-
100-
return self._tab_dz
101-
10293
@property
10394
def _b14(self):
10495
if not hasattr(self, '_b14_'):
@@ -132,7 +123,7 @@ def _roman(self): # pragma: no cover
132123
def run(self):
133124
return
134125

135-
def cSFRD(self, z, Mh):
126+
def get_sfrd_in_mass_range(self, z, Mlo, Mhi=None):
136127
"""
137128
Compute cumulative SFRD as a function of lower-mass bound.
138129
@@ -141,26 +132,23 @@ def cSFRD(self, z, Mh):
141132
Cumulative *FRACTION* of SFRD in halos above Mh.
142133
"""
143134

144-
if type(Mh) not in [list, np.ndarray]:
145-
Mh = np.array([Mh])
146-
147135
iz = np.argmin(np.abs(z - self.histories['z']))
148136
_Mh = self.histories['Mh'][:,iz]
149137
_sfr = self.histories['SFR'][:,iz]
150138
_w = self.histories['nh'][:,iz]
151139

152-
# Really this is the number of galaxies that formed in a given
153-
# differential redshift slice.
154-
SFRD = np.zeros_like(Mh)
155-
for i, _mass in enumerate(Mh):
156-
ok = _Mh >= _mass
157-
SFRD[i] = np.sum(_sfr[ok==1] * _w[ok==1]) / rhodot_cgs
140+
if Mhi is None:
141+
Mhi = _Mh.max()
158142

159-
SFRD_tot = self.SFRD(z)
143+
ok = np.logical_and(_Mh >= Mlo, _Mh <= Mhi)
144+
SFRD = np.sum(_sfr[ok==1] * _w[ok==1]) / rhodot_cgs
160145

161-
return SFRD / SFRD_tot
146+
return SFRD
162147

163148
def SFRD(self, z, Mmin=None):
149+
return self.get_sfrd(z, Mmin=Mmin)
150+
151+
def get_sfrd(self, z, Mmin=None):
164152
"""
165153
Will convert to internal cgs units.
166154
"""
@@ -241,11 +229,11 @@ def tile(self, arr, thin, renorm=False):
241229
else:
242230
return new
243231

244-
def noise_normal(self, arr, sigma):
232+
def get_noise_normal(self, arr, sigma):
245233
noise = np.random.normal(scale=sigma, size=arr.size)
246234
return np.reshape(noise, arr.shape)
247235

248-
def noise_lognormal(self, arr, sigma):
236+
def get_noise_lognormal(self, arr, sigma):
249237
lognoise = np.random.normal(scale=sigma, size=arr.size)
250238
#noise = 10**(np.log10(arr) + np.reshape(lognoise, arr.shape)) - arr
251239
noise = np.power(10, np.log10(arr) + np.reshape(lognoise, arr.shape)) \
@@ -409,11 +397,11 @@ def _gen_halo_histories(self):
409397
# Two potential kinds of scatter in MAR
410398
mar = self.tile(mar_raw, thin)
411399
if sigma_env > 0:
412-
mar *= (1. + self.noise_normal(mar, sigma_env))
400+
mar *= (1. + self.get_noise_normal(mar, sigma_env))
413401

414402
if sigma_mar > 0:
415403
np.random.seed(self.pf['pop_scatter_mar_seed'])
416-
noise = self.noise_lognormal(mar, sigma_mar)
404+
noise = self.get_noise_lognormal(mar, sigma_mar)
417405
mar += noise
418406
# Normalize by mean of log-normal to preserve mean MAR?
419407
mar /= np.exp(0.5 * sigma_mar**2)
@@ -668,18 +656,23 @@ def _TabulateEmissivity(self, E=None, Emin=None, Emax=None, wave=None):
668656

669657
return zarr, tab / cm_per_mpc**3
670658

671-
def Emissivity(self, z, E=None, Emin=None, Emax=None):
659+
def get_emissivity(self, z, E=None, Emin=None, Emax=None):
672660
"""
673661
Compute the emissivity of this population as a function of redshift
674-
and rest-frame photon energy [eV].
662+
and (potentially) rest-frame photon energy [eV].
663+
664+
.. note :: If Emin and Emax are supplied, this is a luminosity density,
665+
and will have units of erg/s/(co-moving cm)^3. If `E` is supplied,
666+
will also carry units of eV^-1.
675667
676668
Parameters
677669
----------
678670
z : int, float
671+
Redshift.
679672
680673
Returns
681674
-------
682-
Emissivity in units of erg / s / c-cm**3 [/ eV]
675+
Emissivity in units of erg / s / c-cm**3 [/ eV].
683676
684677
"""
685678

@@ -707,9 +700,9 @@ def Emissivity(self, z, E=None, Emin=None, Emax=None):
707700
return 10**func(z)
708701
#return self._cache_ehat_[(E, Emin, Emax)](z)
709702

710-
def PhotonLuminosityDensity(self, z, E=None, Emin=None, Emax=None):
703+
def get_photon_density(self, z, E=None, Emin=None, Emax=None):
711704
# erg / s / cm**3
712-
rhoL = self.Emissivity(z, E=E, Emin=Emin, Emax=Emax)
705+
rhoL = self.get_emissivity(z, E=E, Emin=Emin, Emax=Emax)
713706
erg_per_phot = self._get_energy_per_photon(Emin, Emax) * erg_per_ev
714707

715708
return rhoL / np.mean(erg_per_phot)
@@ -1243,7 +1236,7 @@ def _gen_prescribed_galaxy_histories(self, zstop=0):
12431236
noise = np.zeros_like(Sd)
12441237
np.random.seed(self.pf['pop_dust_scatter_seed'])
12451238
for _i, _z in enumerate(z):
1246-
noise[:,_i] = self.noise_lognormal(Sd[:,_i], sigma[:,_i])
1239+
noise[:,_i] = self.get_noise_lognormal(Sd[:,_i], sigma[:,_i])
12471240

12481241
Sd += noise
12491242

@@ -1578,32 +1571,6 @@ def _gen_active_galaxy_histories(self):
15781571

15791572
return results
15801573

1581-
1582-
def Slice(self, z, slc):
1583-
"""
1584-
slice format = {'field': (lo, hi)}
1585-
"""
1586-
1587-
iz = np.argmin(np.abs(z - self.tab_z))
1588-
hist = self.histories
1589-
1590-
c = np.ones(hist['Mh'].shape[0], dtype=int)
1591-
for key in slc:
1592-
lo, hi = slc[key]
1593-
1594-
ok = np.logical_and(hist[key][:,iz] >= lo, hist[key][:,iz] <= hi)
1595-
c *= ok
1596-
1597-
# Build output
1598-
to_return = {}
1599-
for key in self.histories:
1600-
if self.histories[key].ndim == 1:
1601-
to_return[key] = self.histories[key][c==1]
1602-
else:
1603-
to_return[key] = self.histories[key][c==1,iz]
1604-
1605-
return to_return
1606-
16071574
def get_field(self, z, field):
16081575
iz = np.argmin(np.abs(z - self.histories['z']))
16091576
return self.histories[field][:,iz]

tests/test_populations_ensemble.py

+22-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import ares
1414
import numpy as np
15-
from ares.physics.Constants import rhodot_cgs
15+
from ares.physics.Constants import rhodot_cgs, E_LL, cm_per_mpc, ev_per_hz
1616

1717
def test():
1818
pars = ares.util.ParameterBundle('mirocha2020:univ')
@@ -26,14 +26,17 @@ def test():
2626
# Test I/O. Should add more here eventually.
2727
pop.save('test_ensemble', clobber=True)
2828

29+
z = pop.tab_z
30+
t = pop.tab_t
31+
2932
# Test SFRD
30-
sfrd = pop.SFRD(6.) * rhodot_cgs
33+
sfrd = pop.get_sfrd(6.) * rhodot_cgs
3134

3235
assert 1e-3 <= sfrd <= 1, "SFRD unreasonable"
3336

34-
csfrd = pop.cSFRD(6., Mh=1e10)
37+
sfrd10 = pop.get_sfrd_in_mass_range(6., Mlo=1e10)
3538

36-
assert csfrd < 1., "cSFRD >= SFRD!?"
39+
assert sfrd10 < sfrd, "sfrd(Mh>1e10) >= sfrd(all Mh)!?"
3740

3841
logMh, logf_stell, std = pop.get_smhm(6.)
3942
assert np.all(logf_stell < 1)
@@ -144,5 +147,20 @@ def test():
144147
# Test routines to retrieve MUV-Beta, AUV, etc.
145148
AUV = pop.get_AUV(6.)
146149

150+
# Emissivity stuff: just OOM check at the moment.
151+
zarr = np.arange(6, 30)
152+
e_ion = np.array([pop.get_emissivity(z, Emin=E_LL, Emax=1e2) \
153+
for z in zarr]) * cm_per_mpc**3
154+
e_ion2 = np.array([pop.get_emissivity(z, Emin=E_LL, Emax=1e2) \
155+
for z in zarr]) * cm_per_mpc**3 # check caching
156+
157+
assert 1e37 <= np.mean(e_ion) <= 1e41
158+
assert np.allclose(e_ion, e_ion2)
159+
160+
n_ion = np.array([pop.get_photon_density(z, Emin=E_LL, Emax=1e2) \
161+
for z in zarr]) * cm_per_mpc**3
162+
163+
assert 1e47 <= np.mean(n_ion) <= 1e51
164+
147165
if __name__ == '__main__':
148166
test()

0 commit comments

Comments
 (0)