Skip to content

Commit

Permalink
Added function for a(t) calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
aburousan committed Jan 20, 2024
1 parent 1eea17e commit 12931ad
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 61 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ jobs:
Pkg.develop(PackageSpec(path=pwd()))
Pkg.instantiate()
using Documenter: DocMeta, doctest
using cosmic
DocMeta.setdocmeta!(cosmic, :DocTestSetup, :(using cosmic); recursive=true)
doctest(cosmic)
using Cosmic
DocMeta.setdocmeta!(Cosmic, :DocTestSetup, :(using Cosmic); recursive=true)
doctest(Cosmic)
include("docs/make.jl")'
after_success: skip
after_success:
Expand Down
10 changes: 5 additions & 5 deletions CITATION.bib
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
@misc{cosmic.jl,
@misc{Cosmic.jl,
author = {Kazi Abu Rousan},
title = {cosmic.jl},
url = {https://github.com/aburousan/cosmic.jl},
title = {Cosmic.jl},
url = {https://github.com/aburousan/Cosmic.jl},
version = {v1.0.0-DEV},
year = {2023},
month = {11}
year = {2024},
month = {01}
}
34 changes: 34 additions & 0 deletions Examples/general_cos_ex.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Cosmic
using Roots, ForwardDiff
using DifferentialEquations
using Plots, LaTeXStrings

c = cosmology()
ag = age(c,0)
sec_to_year(ag)
z_vals = range(0, 2.5, length=1000)
H_1z(z) = H(c,z)/(1+z)
H_vals = H_1z.(z_vals)
plot(z_vals,H_vals,lw=2.5, label="")
xlabel!(L"Redshift ($z$)")
ylabel!(L"$\frac{H(z)}{1+z}$")
scale_fact(c,1)
# 1.380743453918788e10

tgyr = range(0.1,30.8,length=10_000)
dgf(t) = scale_fact(c,t)
a_vals = dgf.(tgyr)
plot(tgyr,a_vals,lw=2.5, label="")
dgf_part(t) = scalefact_1by1(c,t)
a_vals_part = dgf_part.(tgyr)
plot!(tgyr,a_vals_part,lw=2.5, label="")
H0 = 67.74#(67.74/(3.26*9.5))*1e-18
Ωm = 0.32
ΩΛ = 0.68
Ωγ = 5.35e-5
Ων = 3.64e-5
Ωr = Ωγ + Ων

scalefact_part(c,100)
# ((67.74/(3.26*9.5))*1e-18)*(3600*24*365)/1e-9
# Mpc_to_km(mp_km) = mp_km * 3.262e6 * 9.461e12
11 changes: 7 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name = "cosmic"
name = "Cosmic"
uuid = "a3ac4969-c0e8-4fca-9196-8113a875001f"
authors = ["Kazi Abu Rousan"]
version = "1.0.0-DEV"
Expand All @@ -8,15 +8,18 @@ DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
QuadGK = "1fd47b50-473d-5c70-9696-f719f8f3bcdc"
ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267"
Roots = "f2b01f46-fcfa-551c-844a-d8ac1e96c665"

[compat]
julia = "1"

DifferentialEquations = "7.12.0"
ForwardDiff = "0.10.36"
ReverseDiff = "1.15.1"
Roots = "2.0.22"
QuadGK = "2.9.4 , 2.9.3 , 2.9.2"
[extras]
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"

[targets]
test = ["Test"]
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# cosmic
# Cosmic

[![Stable](https://img.shields.io/badge/docs-stable-blue.svg)](https://aburousan.github.io/cosmic.jl/stable/)
[![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://aburousan.github.io/cosmic.jl/dev/)
[![Build Status](https://github.com/aburousan/cosmic.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/aburousan/cosmic.jl/actions/workflows/CI.yml?query=branch%3Amain)
[![Build Status](https://travis-ci.com/aburousan/cosmic.jl.svg?branch=main)](https://travis-ci.com/aburousan/cosmic.jl)
[![Build Status](https://ci.appveyor.com/api/projects/status/github/aburousan/cosmic.jl?svg=true)](https://ci.appveyor.com/project/aburousan/cosmic-jl)
[![Coverage](https://codecov.io/gh/aburousan/cosmic.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/aburousan/cosmic.jl)
[![Coverage](https://coveralls.io/repos/github/aburousan/cosmic.jl/badge.svg?branch=main)](https://coveralls.io/github/aburousan/cosmic.jl?branch=main)
[![Stable](https://img.shields.io/badge/docs-stable-blue.svg)](https://aburousan.github.io/Cosmic.jl/stable/)
[![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://aburousan.github.io/Cosmic.jl/dev/)
[![Build Status](https://github.com/aburousan/Cosmic.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/aburousan/Cosmic.jl/actions/workflows/CI.yml?query=branch%3Amain)
[![Build Status](https://travis-ci.com/aburousan/Cosmic.jl.svg?branch=main)](https://travis-ci.com/aburousan/Cosmic.jl)
[![Build Status](https://ci.appveyor.com/api/projects/status/github/aburousan/Cosmic.jl?svg=true)](https://ci.appveyor.com/project/aburousan/Cosmic-jl)
[![Coverage](https://codecov.io/gh/aburousan/Cosmic.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/aburousan/Cosmic.jl)
[![Coverage](https://coveralls.io/repos/github/aburousan/Cosmic.jl/badge.svg?branch=main)](https://coveralls.io/github/aburousan/Cosmic.jl?branch=main)
9 changes: 9 additions & 0 deletions src/Cosmic.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Cosmic

include("basic_cos.jl")

export a, redshift, cosmology, Mpc_to_km, sec_to_year, H, my_f, my_g,
hubble_distance, χ, hubble_time, T, age, look_back_time, scale_fact,
da_dt, ageGyr,scalefact_part

end# module end
75 changes: 55 additions & 20 deletions src/basic_cos.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using QuadGK




using QuadGK, Roots
using DifferentialEquations
#-----------------------------------------------------------------------------------
# Abstract types for better calculation
abstract type AbstractCosmology end
abstract type AbstractClosedCosmology <: AbstractCosmology end
abstract type AbstractFlatCosmology <: AbstractCosmology end
abstract type AbstractOpenCosmology <: AbstractCosmology end


# For flat ΛCDM
struct FlatLCDM{T <: Real} <: AbstractFlatCosmology
h::T
Expand Down Expand Up @@ -129,26 +127,63 @@ hubble_distance(c::AbstractCosmology, z) = χ0(c) / H_by_H0(c, z)# 1/H -> hubble
hubble_time(c::AbstractCosmology, z) = η0(c) / H_by_H0(c, z)
T(c::AbstractCosmology, a0, a1; kws...) = QuadGK.quadgk(x->x / H_a2_by_H0(c, x), a0, a1; kws...)[1]
age(c::AbstractCosmology, z; kws...) = η0(c) * T(c, 0, a(z); kws...)
lookback_time(c::AbstractCosmology, z; kws...) = η0(c) * T(c, a(z), 1; kws...)
# Constants all are in MeV and C = 1, ħ = 1, kᵦ = 1
# H0 = (67.74/(3.26*9.5))*1e-18
# Ωm = 0.32
# ΩΛ = 0.68
# Ωγ = 5.35e-5
# Ων = 3.64e-5
# Ωr = Ωγ + Ων
# zeq = 3395
# All constants are from Baumann's book
ageGyr(c::AbstractCosmology,z;kws...) = sec_to_year(age(c,z))*1e-9
look_back_time(c::AbstractCosmology, z; kws...) = η0(c) * T(c, a(z), 1; kws...)

function da_dt(p::AbstractCosmology,u)#Note t in Gyr
H0 = 219495e-27# per gigayear
fact = H_a2_by_H0(p,u)/u
return H0*fact
end

scalefact(c::AbstractCosmology,z1) = find_zero(age(c,z) - z1, (1100,1300))
function scale_fact(c::AbstractCosmology,t,series = false)
H0 = ((c.h*100/(3.26*9.5)))*(3600*24*365e-9)#219495e-27# per gigayear
dadt(u,p,t) = H0*H_a2_by_H0(c,u)/u
tspan = (0, t)
u0 = 1e-8
prob = ODEProblem(dadt,u0, tspan)
sol = solve(prob,Vern7())
if series
return sol
else
return sol(t)
end
end

function scalefact_part(c::AbstractFlatCosmology,t)
# t = time/1e9
H0 = ((c.h*100/(3.26*9.5)))*(3600*24*365e-9)
if t <= 2e5/1e9
return sqrt(2*H0*t)*(c.Ω_r)^(1/4)
elseif t > 2e5/1e9 && t <= 1.5e9/1e9
return (3/2)^(2/3)*(H0*t)^(2/3)*(c.Ω_m)^(1/3)
else
return (1/c.Ω_Λ - 1)^(1/3)*(sinh(3*H0*t(c.Ω_Λ)/2))^(2/3)
end

function inte_for_age(a)
deno = (Ωr/a^2 + Ωm/a + ΩΛ*a^2)
return 1/deno
end
# 2 + 3im

# tspan = (1e10, 1e0)
# prob = ODEProblem(da_dt, u0, tspan)
# sol = solve(prob, Rodas5(), reltol=1e-12, abstol=1e-12)# Tsit5(), reltol = 1e-8, abstol = 1e-8)
# t_bol = sort(sol.t)#solution Z values
# a_bol = sort(sol.u)#solution Xe values

age(a) = QuadGK.quadgk(inte_for_age, 0, a)[1]/H0 # In sec
# # find_zero(pebb_05, (1100,1300))
# # find_zero(f, 0.6)
# # Constants all are in MeV and C = 1, ħ = 1, kᵦ = 1
# H0 = (67.74/(3.26*9.5))*1e-18
# 67.74/(3.262e6 * 9.461e12)
# # Ωm = 0.32
# # ΩΛ = 0.68
# # Ωγ = 5.35e-5
# # Ων = 3.64e-5
# # Ωr = Ωγ + Ων
# # zeq = 3395
# # All constants are from Baumann's book
# sec_to_year((67.74/(3.26*9.5))*1e-18)
my_f(x,y) = 2x+7y

my_g(x) = x^2
Expand Down
8 changes: 0 additions & 8 deletions src/cosmic.jl

This file was deleted.

19 changes: 6 additions & 13 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
using cosmic
using Cosmic
using Test

@testset "cosmic.jl" begin
@testset "Cosmic.jl" begin
@test my_f(2,1) == 11
@test my_g(2) == 4
@test redshift(2.9e-4) 3447.27586
end

using Plots, LaTeXStrings

c = cosmology()
z_vals = range(0, 2.5, length=1000)
H_1z(z) = H(c,z)/(1+z)
H_vals = H_1z.(z_vals)
plot(z_vals,H_vals,lw=2.5, label="")
xlabel!(L"Redshift ($z$)")
ylabel!(L"$\frac{H(z)}{1+z}$")
@test cosmology().h 0.6774
@test scalefact_part(cosmology(),100) 148.9412838881
@test ageGyr(cosmology(),0) 13.80743453918
end

0 comments on commit 12931ad

Please sign in to comment.