From 12931ad0deb81d6237f56641149f7a872e9ee8f1 Mon Sep 17 00:00:00 2001 From: Kazi Abu Rousan Date: Sun, 21 Jan 2024 00:39:39 +0530 Subject: [PATCH] Added function for a(t) calculation --- .travis.yml | 6 +-- CITATION.bib | 10 ++--- Examples/general_cos_ex.jl | 34 +++++++++++++++++ Project.toml | 11 ++++-- README.md | 16 ++++---- src/Cosmic.jl | 9 +++++ src/basic_cos.jl | 75 ++++++++++++++++++++++++++++---------- src/cosmic.jl | 8 ---- test/runtests.jl | 19 +++------- 9 files changed, 127 insertions(+), 61 deletions(-) create mode 100644 Examples/general_cos_ex.jl create mode 100644 src/Cosmic.jl delete mode 100644 src/cosmic.jl diff --git a/.travis.yml b/.travis.yml index c99804b..20caecb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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: diff --git a/CITATION.bib b/CITATION.bib index 3a82c56..c302337 100644 --- a/CITATION.bib +++ b/CITATION.bib @@ -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} } diff --git a/Examples/general_cos_ex.jl b/Examples/general_cos_ex.jl new file mode 100644 index 0000000..316d9ac --- /dev/null +++ b/Examples/general_cos_ex.jl @@ -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 \ No newline at end of file diff --git a/Project.toml b/Project.toml index cc33c8f..aee44c0 100644 --- a/Project.toml +++ b/Project.toml @@ -1,4 +1,4 @@ -name = "cosmic" +name = "Cosmic" uuid = "a3ac4969-c0e8-4fca-9196-8113a875001f" authors = ["Kazi Abu Rousan"] version = "1.0.0-DEV" @@ -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"] diff --git a/README.md b/README.md index f7fc15a..5fbfab6 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/Cosmic.jl b/src/Cosmic.jl new file mode 100644 index 0000000..3db6d1c --- /dev/null +++ b/src/Cosmic.jl @@ -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 diff --git a/src/basic_cos.jl b/src/basic_cos.jl index 3daab04..f4b8812 100644 --- a/src/basic_cos.jl +++ b/src/basic_cos.jl @@ -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 @@ -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 diff --git a/src/cosmic.jl b/src/cosmic.jl deleted file mode 100644 index 468fd14..0000000 --- a/src/cosmic.jl +++ /dev/null @@ -1,8 +0,0 @@ -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, lookback_time - -end# module end diff --git a/test/runtests.jl b/test/runtests.jl index a703380..de51fb0 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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}$") \ No newline at end of file + @test cosmology().h ≈ 0.6774 + @test scalefact_part(cosmology(),100) ≈ 148.9412838881 + @test ageGyr(cosmology(),0) ≈ 13.80743453918 +end \ No newline at end of file