Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type stabilities for subtypes of AbstractCosmology #46

Open
astrozot opened this issue Sep 21, 2023 · 0 comments
Open

Type stabilities for subtypes of AbstractCosmology #46

astrozot opened this issue Sep 21, 2023 · 0 comments

Comments

@astrozot
Copy link
Contributor

The code makes use of several subtypes of AbstractCosmology for different cosmological models: (Closed|Flat|Open)LCDM and (Closed|Flat|Open)WCDM. However, it seems to me that are (if any) very little advantages in doing so, while there are possible disadvantages.

There are two main reasons to have different subtypes:

  • The comoving_transverse_dist and comoving_volume functions are (slightly) different depending on the spatial curvature
  • The function a2E can be (very slightly) simplified in case of a flat universe

On the other hand, in some cases one might want to store a cosmological model in a variable, and in that case one is essentially forced to use an abstract type, with the associated penalties in terms of efficiency. Such a situation, for example, arises in case one is interested in performing an inference on the cosmological parameters: in that case one would end up by having the associated cosmological model as an abstract type, which in turns would induce type instabilities in many associated parts of the code.

For this reason, I propose to consider the use of a structure that could accommodate closed, flat, and open cosmological models. In order to avoid making breaking changes, one could add new subtypes of AbstractCosmology:

abstract type AbstractGeneralCosmology <: AbstractCosmology end

struct GeneralLCDM{T <: Real} <: AbstractGeneralCosmology
    h::T
    Ω_k::T
    Ω_Λ::T
    Ω_m::T
    Ω_r::T
end

struct GeneralWCDM{T <: Real} <: AbstractGeneralCosmology
    h::T
    Ω_k::T
    Ω_Λ::T
    Ω_m::T
    Ω_r::T
    w0::T
    wa::T
end

It would then be trivial to write the relevant functions by just checking the signs of Ω_k in these fields. That would allow one to write type-stable code regardless of the value of the spatial curvature. Note that by just adding these two types one would not break existing code: so, in the usual case where one fixes the cosmological model, one would work with the current types. The general cosmology types could be returned by a specific function similar to cosmology (called, for example, general_cosmology or something similar).

In case there is consensus on this, I would be happy to contribute with the relevant code.

P.S. The other alternative could be to also remove the old (Closed|Flat|Open) types: that might break current code, in case one makes explicit reference to the cosmology type, but I guess that would be a minority of situations. Still, it is probably wiser not to make breaking changes...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant