Skip to content

Commit 1116b89

Browse files
Jammy2211claude
authored andcommitted
add luminosity_distance to LensingCosmology
Implements D_L(z) = (1+z)^2 * D_A(0,z) returning Mpc, matching astropy's convention. Fixes AttributeError in workspace scripts that call cosmo.luminosity_distance(). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent d348d13 commit 1116b89

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

autogalaxy/cosmology/model.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,27 @@ def kpc_per_arcsec_from(self, redshift: float, xp=np) -> float:
6262
"""
6363
return self.kpc_proper_per_arcsec(z=redshift, xp=xp)
6464

65+
def luminosity_distance(self, z: float, xp=np) -> float:
66+
"""
67+
Luminosity distance to redshift z in Mpc.
68+
69+
For a flat universe:
70+
71+
D_L(z) = (1 + z)^2 * D_A(0, z)
72+
73+
where D_A(0, z) is the angular diameter distance from Earth.
74+
75+
Returns Mpc, matching the convention of astropy.cosmology.FlatLambdaCDM.luminosity_distance(z).value.
76+
77+
Parameters
78+
----------
79+
z
80+
Redshift at which the luminosity distance is calculated.
81+
"""
82+
D_A_kpc = self.angular_diameter_distance_to_earth_in_kpc_from(redshift=z, xp=xp)
83+
D_A_Mpc = D_A_kpc / xp.asarray(1.0e3)
84+
return (xp.asarray(1.0) + xp.asarray(z)) ** 2 * D_A_Mpc
85+
6586
def angular_diameter_distance_to_earth_in_kpc_from(
6687
self, redshift: float, xp=np
6788
) -> float:

test_autogalaxy/cosmology/test_model.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,17 @@ def test__scaling_factor_between_redshifts_from():
115115
assert scaling_factor == pytest.approx(0.6739452581456, 1e-4)
116116

117117

118+
def test__luminosity_distance():
119+
120+
cosmology = ag.cosmology.Planck15()
121+
122+
# D_A(0, 0.1) = 392840 kpc = 392.840 Mpc (verified by test__angular_diameter_distances)
123+
# D_L(0.1) = (1 + 0.1)^2 * 392.840 = 1.21 * 392.840 = 475.3364 Mpc
124+
luminosity_distance = cosmology.luminosity_distance(z=0.1)
125+
126+
assert luminosity_distance == pytest.approx(475.336, 1e-4)
127+
128+
118129
def test__velocity_dispersion_from():
119130

120131
cosmology = ag.cosmology.Planck15()

0 commit comments

Comments
 (0)