GadgetUnits.jl

Author LudwigBoess
Popularity
2 Stars
Updated Last
1 Year Ago
Started In
July 2020
Documentation Build Status License
Run CI on master codecov.io The MIT License

GadgetUnits.jl

This package is a subproject of GadJet.jl and provides some basic unit conversion functionality to work with the SPH code "Gadget" by Volker Springel (doi:10.1111/j.1365-2966.2005.09655.x).

Unit Conversion

GadgetUnits.jl uses Unitful.jl and UnitfulAstro.jl to store the unit conversion factors with actual units in place. You can convert the internal units of Gadget into cgs units by defining the object GadgetPhysicalUnits:

GU = GadgetPhysicalUnits(l_unit::Float64=3.085678e21, m_unit::Float64=1.989e43, v_unit::Float64=1.e5;
                         a_scale::Float64=1.0, hpar::Float64=1.0,
                         γ_th::Float64=5.0/3.0, γ_CR::Float64=4.0/3.0, xH::Float64=0.76)

where the keyword arguments are:

  • a_scale::Float64 = 1.0: Cosmological scale factor of the simulation. Can be passed with the header h as h.time.
  • hpar::Float64 = 1.0: Hubble constant as 'little h'. Can be passed with header h as h.h0.
  • γ_th::Float64 = 5.0/3.0: Adiabatic index of gas.
  • γ_CR::Float64 = 4.0/3.0: Adiabatic index of cosmic ray component.
  • xH::Float64 = 0.76: Hydrogen fraction of the simulation, if run without chemical model.

This returns an object of type GadgetPhysicalUnits with the following properties:

struct GadgetPhysicalUnits

    x_cgs::typeof(1.0u"cm")         # position in cm
    x_kpc::typeof(1.0u"kpc")        # position in kpc

    v_cgs::typeof(1.0u"cm/s")       # velocity in cm/s
    v_kms::typeof(1.0u"km/s")       # velocity in km/s

    m_cgs::typeof(1.0u"g")          # mass in g
    m_msun::typeof(1.0u"Msun")      # mass in Msun

    t_s::typeof(1.0u"s")            # time in sec
    t_Myr::typeof(1.0u"Myr")        # time in Myr

    E_cgs::typeof(1.0u"erg")        # energy in erg
    E_eV::typeof(1.0u"eV")          # energy in eV

    B_cgs::typeof(1.0u"Gs")         # magnetic field in Gauss

    rho_cgs::typeof(1.0u"g/cm^3")   # density in g/cm^3
    rho_ncm3::typeof(1.0u"mp/cm^3") # density in mp/cm^3

    T_K::typeof(1.0u"K")            # temperature in K
    T_eV::typeof(1.0u"eV")          # temperature in eV

    P_th_cgs::typeof(1.0u"Ba")      # thermal pressure in Ba
    P_CR_cgs::typeof(1.0u"Ba")      # cosmic ray pressure in Ba

end

To convert, say positions of gas particles from a cosmological simulation to physical units you can use:

h     = read_header(filename)

pos   = read_snap(filename, "POS", 0)

GU    = GadgetPhysicalUnits(a_scale=h.time, hpar=h.h0)

pos .*= GU.x_cgs

If you have different units than the standard Gadget ones you can call the object cunstructor with different values

GU = GadgetPhysicalUnits(your_l_unit, your_m_unit, your_v_unit; kwargs...)

Converting the units can then be done with Unitful.jl and UnitfulAstro.jl. So if you want to convert the position units from the default cm to Mpc you can do this as:

using Unitful
using UnitfulAstro

pos = read_snap(filename, "POS", 0)
pos = @. pos * GU.x_cgs |> u"Mpc"

If you want to get rid of the units, for example if you need basic datatypes again for a function you can use the funtion ustrip:

pos = ustrip(pos)

Primitive unit type

If you want to have the same functionality, but without using Unitful.jl you can construct a similar object:

GU = GadgetPhysical(l_unit::Float64=3.085678e21, m_unit::Float64=1.989e43, v_unit::Float64=1.e5;
                    a_scale::Float64=1.0, hpar::Float64=1.0,
                    γ_th::Float64=5.0/3.0, γ_CR::Float64=4.0/3.0, xH::Float64=0.76)

This uses the same conversions, but leaves out the actual unit strings.

Used By Packages

No packages found.