Skip to content

Integration with Unitful.jl #6

@oschulz

Description

@oschulz

Geant4.jl currently has it's own implementation of physical units in Geant4.SystemOfUnits. It would be nice to integrate Geant4 with Unitful.jl since it's pretty much the standard for units in the Julia ecosystem.

We could define

using Unitfil

const g4length = u"mm"
const g4area = g4length^2
const g4volume = g4length^3
const g4time = u"ns"
const g4angle = u"rad"
const g4solidangle = u"sr"
const g4frequency = inv(g4time)
const g4charge = u"q"
const g4energy = u"MeV"
const g4mass = g4energy*g4time*g4time/(g4length*g4length) # Simplify?
const g4density = g4mass/g4volume

# Same for: power, force, pressure, current, epot, resistance, capacitance, flux, magnetic_field,
# inductance, temperature, amount, activity, dose, intensity, luminous_flux, illuminance

and maybe also convenience functions

asg4length(x::Unitful.Quantity{<:Real}) = ustrip(g4_length, x)
asg4area(x::Unitful.Quantity{<:Real}) = ustrip(g4area, x)
asg4volume(x::Unitful.Quantity{<:Real}) = ustrip(g4volume, x)
asg4time(x::Unitful.Quantity{<:Real}) = ustrip(g4time, x)
asg4angle(x::Unitful.Quantity{<:Real}) = ustrip(g4angle, x)
asg4solidangle(x::Unitful.Quantity{<:Real}) = ustrip(g4solidangle, x)
asg4frequency(x::Unitful.Quantity{<:Real}) = ustrip(g4frequency, x)
asg4charge(x::Unitful.Quantity{<:Real}) = ustrip(g4charge, x)
asg4energy(x::Unitful.Quantity{<:Real}) = ustrip(g4energy, x)
asg4mass(x::Unitful.Quantity{<:Real}) = ustrip(g4mass, x)
asg4density(x::Unitful.Quantity{<:Real}) = ustrip(g4density, x)
# ...


asg4length(x::Real) = x
asg4area(x::Real) = x
asg4volume(x::Real) = x
asg4time(x::Real) = x
asg4angle(x::Real) = x
asg4solidangle(x::Real) = x
asg4frequency(x::Real) = x
asg4charge(x::Real) = x
asg4energy(x::Real) = x
asg4mass(x::Real) = x
asg4mass(x::Real) = x
# ...

That way, we could use generic code like

const G4RealQuantity{T<:Real} = Union{T,Unitful.Quantity{<:T}}

G4Material(name::String; #=...=#, density::G4RealQuantity, #=...=#) = #=...=# asg4density(density) #=...=#

in "G4JLInterface.jl". Users could then write either

using Unitful
G4Material("quartz", density=2.200*u"g/cm^3", ncomponents=2)

or

using Geant4.SystemOfUnits: g, cm
G4Material("quartz", density=2.200g/cm3, ncomponents=2)

or use unitless numbers directly

using Geant4.SystemOfUnits: g, cm
G4Material("quartz", density=1.3731319963813677e19, ncomponents=2)

Or should we try to replace Geant4.SystemOfUnits by Unitful (aliases) completely?

@giordano, you have a lot of experience with Unitful.jl and derived packages, do you think the design above goes in the right direction?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions