-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 58ebbee
Showing
5 changed files
with
303 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2013 Mike Nolta <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a | ||
copy of this software and associated documentation files (the "Software"), | ||
to deal in the Software without restriction, including without limitation | ||
the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
and/or sell copies of the Software, and to permit persons to whom the | ||
Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
Cosmology calculator for Julia | ||
============================== | ||
|
||
Installation | ||
------------ | ||
|
||
To install the package: | ||
|
||
julia> Pkg.add("Cosmology") | ||
|
||
Then, to load into your session: | ||
|
||
julia> using Cosmology | ||
|
||
Cosmological Models | ||
------------------- | ||
|
||
First, pick a cosmological model using the `cosmology` function, | ||
which takes the following options: | ||
|
||
<table> | ||
<tr> | ||
<td>h = 0.7</td> | ||
<td>Dimensionless Hubble constant</td> | ||
</tr> | ||
<tr> | ||
<td>OmegaK = 0</td> | ||
<td>Curvature density, Ω<sub>k</sub></td> | ||
</tr> | ||
<tr> | ||
<td>OmegaM = 0.3</td> | ||
<td>Matter density, Ω<sub>m</sub></td> | ||
</tr> | ||
<tr> | ||
<td>OmegaR = Ω<sub>γ</sub> + Ω<sub>ν</sub></td> | ||
<td>Radiation density, Ω<sub>r</sub></td> | ||
</tr> | ||
<tr> | ||
<td>Tcmb = 2.7255</td> | ||
<td>CMB temperature (K), used to compute Ω<sub>γ</sub></td> | ||
</tr> | ||
<tr> | ||
<td>Neff = 3.04</td> | ||
<td>Effective number of massless neutrino species, used to compute Ω<sub>ν</sub></td> | ||
</tr> | ||
</table> | ||
|
||
```jlcon | ||
julia> c = cosmology() | ||
FlatLCDM(0.7,0.6999146929857499,0.3,8.530701425005273e-5) | ||
julia> c = cosmology(OmegaK=0.1) | ||
OpenLCDM(0.7,0.1,0.5999146929857501,0.3,8.530701425005273e-5) | ||
``` | ||
|
||
Distances | ||
--------- | ||
|
||
<table> | ||
<tr> | ||
<td>angular_diameter_dist_mpc(cosmo, z)</td> | ||
<td>Ratio of an object's proper transverse size (in Mpc) to its angular size (in radians)</td> | ||
</tr> | ||
<tr> | ||
<td>comoving_radial_dist_mpc(cosmo, z)</td> | ||
<td>Comoving radial distance to redshift z, in Mpc</td> | ||
</tr> | ||
<tr> | ||
<td>luminosity_dist_mpc(cosmo, z)</td> | ||
<td>Bolometric luminosity distance, in Mpc</td> | ||
</tr> | ||
</table> | ||
|
||
```jlcon | ||
julia> using Cosmology | ||
julia> c = cosmology(OmegaM=0.26) | ||
FlatLCDM(0.7,0.739914695489689,0.26,8.530451031095114e-5) | ||
julia> angular_diameter_dist_mpc(c, 1.2) | ||
1758.5291281199122 | ||
``` | ||
|
||
Times | ||
----- | ||
|
||
<table> | ||
<tr> | ||
<td>age_gyr(cosmo, z)</td> | ||
<td>Age of the universe at redshift z, in Gyr</td> | ||
</tr> | ||
<tr> | ||
<td>lookback_time_gyr(cosmo, z)</td> | ||
<td>Difference between age at redshift 0 and age at redshift z, in Gyr</td> | ||
</tr> | ||
</table> | ||
|
||
```jlcon | ||
julia> using Cosmology | ||
julia> c = cosmology(OmegaM=0.26) | ||
FlatLCDM(0.7,0.739914695489689,0.26,8.530451031095114e-5) | ||
julia> age_gyr(c, 1.2) | ||
5.367964753127867 | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
julia 0.2- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
module Cosmology | ||
|
||
export cosmology, | ||
age_gyr, | ||
angular_diameter_dist_mpc, | ||
comoving_radial_dist_mpc, | ||
comoving_transverse_dist_mpc, | ||
H, | ||
hubble_dist_mpc, | ||
hubble_time_gyr, | ||
luminosity_dist_mpc, | ||
lookback_time_gyr, | ||
scale_factor | ||
|
||
abstract AbstractCosmology | ||
abstract AbstractClosedCosmology <: AbstractCosmology | ||
abstract AbstractFlatCosmology <: AbstractCosmology | ||
abstract AbstractOpenCosmology <: AbstractCosmology | ||
|
||
immutable FlatLCDM <: AbstractFlatCosmology | ||
h::Float64 | ||
Ω_Λ::Float64 | ||
Ω_m::Float64 | ||
Ω_r::Float64 | ||
|
||
function FlatLCDM(h::Real, Ω_Λ::Real, Ω_m::Real, Ω_r::Real) | ||
new(float64(h), float64(Ω_Λ), float64(Ω_m), float64(Ω_r)) | ||
end | ||
end | ||
|
||
a2E(c::FlatLCDM, a::Float64) = sqrt(c.Ω_r + c.Ω_m*a + c.Ω_Λ*a^4) | ||
|
||
immutable ClosedLCDM <: AbstractClosedCosmology | ||
h::Float64 | ||
Ω_k::Float64 | ||
Ω_Λ::Float64 | ||
Ω_m::Float64 | ||
Ω_r::Float64 | ||
|
||
function ClosedLCDM(h::Real, Ω_k::Real, Ω_Λ::Real, Ω_m::Real, Ω_r::Real) | ||
new(float64(h), float64(Ω_k), float64(Ω_Λ), float64(Ω_m), float64(Ω_r)) | ||
end | ||
end | ||
|
||
immutable OpenLCDM <: AbstractOpenCosmology | ||
h::Float64 | ||
Ω_k::Float64 | ||
Ω_Λ::Float64 | ||
Ω_m::Float64 | ||
Ω_r::Float64 | ||
|
||
function OpenLCDM(h::Real, Ω_k::Real, Ω_Λ::Real, Ω_m::Real, Ω_r::Real) | ||
new(float64(h), float64(Ω_k), float64(Ω_Λ), float64(Ω_m), float64(Ω_r)) | ||
end | ||
end | ||
|
||
function a2E(c::Union(ClosedLCDM,OpenLCDM), a::Float64) | ||
a2 = a*a | ||
sqrt(c.Ω_r + c.Ω_m*a + (c.Ω_k + c.Ω_Λ*a2)*a2) | ||
end | ||
|
||
function cosmology(;h=0.7, | ||
Neff=3.04, | ||
OmegaK=0, | ||
OmegaM=0.3, | ||
OmegaR=nothing, | ||
Tcmb=2.7255) | ||
|
||
if OmegaR === nothing | ||
OmegaG = 4.48131e-7*Tcmb^4/h^2 | ||
OmegaN = Neff*OmegaG*(7/8)*(4/11)^(4/3) | ||
OmegaR = OmegaG + OmegaN | ||
end | ||
|
||
OmegaL = 1. - OmegaK - OmegaM - OmegaR | ||
|
||
if OmegaK < 0 | ||
return ClosedLCDM(h, OmegaK, OmegaL, OmegaM, OmegaR) | ||
elseif OmegaK > 0 | ||
return OpenLCDM(h, OmegaK, OmegaL, OmegaM, OmegaR) | ||
else | ||
return FlatLCDM(h, OmegaL, OmegaM, OmegaR) | ||
end | ||
end | ||
|
||
# hubble rate | ||
|
||
scale_factor(z) = 1/(1 + z) | ||
E(c::AbstractCosmology, z) = (a = scale_factor(z); a2E(c,a)/a^2) | ||
H(c::AbstractCosmology, z) = 100. * c.h * E(c, z) | ||
|
||
hubble_dist_mpc0(c::AbstractCosmology) = 2997.92458/c.h | ||
hubble_dist_mpc(c::AbstractCosmology, z) = hubble_dist_mpc0(c)/E(c,z) | ||
|
||
hubble_time_gyr0(c::AbstractCosmology) = 9.77814/c.h | ||
hubble_time_gyr(c::AbstractCosmology, z) = hubble_time_gyr0(c)/E(c,z) | ||
|
||
# distances | ||
|
||
Z(c::AbstractCosmology, z::Real) = ((q,_) = quadgk(a::Float64->1.0/a2E(c,a), scale_factor(z), 1); q) | ||
|
||
comoving_radial_dist_mpc(c::AbstractCosmology, z) = hubble_dist_mpc0(c)*Z(c, z) | ||
|
||
comoving_transverse_dist_mpc(c::AbstractFlatCosmology, z) = | ||
comoving_radial_dist_mpc(c, z) | ||
function comoving_transverse_dist_mpc(c::AbstractOpenCosmology, z) | ||
sqrtok = sqrt(c.Ω_k) | ||
hubble_dist_mpc0(c)*sinh(sqrtok*Z(c,z))/sqrtok | ||
end | ||
function comoving_transverse_dist_mpc(c::AbstractClosedCosmology, z) | ||
sqrtok = sqrt(abs(c.Ω_k)) | ||
hubble_dist_mpc0(c)*sin(sqrtok*Z(c,z))/sqrtok | ||
end | ||
|
||
angular_diameter_dist_mpc(c::AbstractCosmology, z) = | ||
comoving_transverse_dist_mpc(c, z)/(1 + z) | ||
|
||
luminosity_dist_mpc(c::AbstractCosmology, z) = | ||
comoving_transverse_dist_mpc(c, z)*(1 + z) | ||
|
||
# times | ||
|
||
T(c::AbstractCosmology, a0::Float64, a1::Float64) = ((q,_) = quadgk(x::Float64->x/a2E(c,x), a0, a1); q) | ||
age_gyr(c::AbstractCosmology, z) = hubble_time_gyr0(c)*T(c, 0., scale_factor(z)) | ||
lookback_time_gyr(c::AbstractCosmology, z) = hubble_time_gyr0(c)*T(c, scale_factor(z), 1.) | ||
|
||
end # module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using Cosmology | ||
using Base.Test | ||
|
||
function test_approx_eq_rtol(va, vb, rtol, astr, bstr) | ||
diff = max(abs(va - vb)) | ||
tol = rtol*max(max(abs(va)), max(abs(vb))) | ||
if diff > tol | ||
sdiff = string("|", astr, " - ", bstr, "| <= ", tol) | ||
error("assertion failed: ", sdiff, | ||
"\n ", astr, " = ", va, | ||
"\n ", bstr, " = ", vb, | ||
"\n difference = ", diff, " > ", tol) | ||
end | ||
end | ||
|
||
macro test_approx_eq_rtol(a, b, c) | ||
:(test_approx_eq_rtol($(esc(a)), $(esc(b)), $(esc(c)), $(string(a)), $(string(b)))) | ||
end | ||
|
||
# values from http://icosmos.co.uk/ | ||
|
||
dist_rtol = 1e-6 | ||
age_rtol = 2e-4 | ||
|
||
c = cosmology(h=0.7, OmegaM=0.3, OmegaR=0) | ||
@test_approx_eq_rtol angular_diameter_dist_mpc(c,1) 1651.9145 dist_rtol | ||
@test_approx_eq_rtol comoving_radial_dist_mpc(c,1) 3303.829 dist_rtol | ||
@test_approx_eq_rtol luminosity_dist_mpc(c,1) 6607.6579 dist_rtol | ||
@test_approx_eq_rtol age_gyr(c,0) 13.4694 age_rtol | ||
@test_approx_eq_rtol age_gyr(c,1) 5.7527 age_rtol | ||
@test_approx_eq_rtol lookback_time_gyr(c,1) 13.4694-5.7527 age_rtol | ||
|
||
c = cosmology(h=0.7, OmegaK=0.1, OmegaM=0.3, OmegaR=0) | ||
@test_approx_eq_rtol angular_diameter_dist_mpc(c,1) 1619.9588 dist_rtol | ||
@test_approx_eq_rtol comoving_radial_dist_mpc(c,1) 3209.784 dist_rtol | ||
@test_approx_eq_rtol luminosity_dist_mpc(c,1) 6479.8352 dist_rtol | ||
@test_approx_eq_rtol age_gyr(c,0) 13.064 age_rtol | ||
@test_approx_eq_rtol age_gyr(c,1) 5.5466 age_rtol | ||
@test_approx_eq_rtol lookback_time_gyr(c,1) 13.064-5.5466 age_rtol | ||
|
||
c = cosmology(h=0.7, OmegaK=-0.1, OmegaM=0.3, OmegaR=0) | ||
@test_approx_eq_rtol angular_diameter_dist_mpc(c,1) 1686.5272 dist_rtol | ||
@test_approx_eq_rtol comoving_radial_dist_mpc(c,1) 3408.937 dist_rtol | ||
@test_approx_eq_rtol luminosity_dist_mpc(c,1) 6746.1088 dist_rtol | ||
@test_approx_eq_rtol age_gyr(c,0) 13.925 age_rtol | ||
@test_approx_eq_rtol age_gyr(c,1) 5.9868 age_rtol | ||
@test_approx_eq_rtol lookback_time_gyr(c,1) 13.925-5.9868 age_rtol |