Skip to content

Commit

Permalink
refactor: rename Particle interface for creating particles to `part…
Browse files Browse the repository at this point in the history
…icle`, and add functionality to process special particles
  • Loading branch information
Beforerr committed Nov 19, 2024
1 parent d89c67e commit bac2c13
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 69 deletions.
1 change: 1 addition & 0 deletions src/ChargedParticles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ using Match
using Unitful: me, mp

include("./types.jl")
include("./particle.jl")
include("./properties.jl")
include("./aliases.jl")
include("./utils.jl")
Expand Down
6 changes: 3 additions & 3 deletions src/aliases.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ Each entry maps a string alias to a tuple of (symbol, charge, mass_number)
PARTICLE_ALIASES = Dict(
"e+" => ("e", 1, 0),
"positron" => ("e", 1, 0),
"neutron" => ("n", 0, 1),
"n" => ("n", 0, 1),
"neutron" => :Neutron,
"n" => :Neutron,
"alpha" => ("He", 2, 4),
"deuteron" => ("H", 1, 2),
"D+" => ("H", 1, 2),
Expand All @@ -27,6 +27,6 @@ PARTICLE_ALIASES = Dict(
"mu+" => ("μ", 1, 0),
)

ELECTRON_ALIASES_DICT = Dict(str => ("e", -1, 0) for str in ELECTRON_ALIASES)
ELECTRON_ALIASES_DICT = Dict(str => :Electron for str in ELECTRON_ALIASES)
PROTON_ALIASES_DICT = Dict(str => ("H", 1, 1) for str in PROTON_ALIASES)
PARTICLE_ALIASES = merge(PARTICLE_ALIASES, ELECTRON_ALIASES_DICT, PROTON_ALIASES_DICT)
73 changes: 73 additions & 0 deletions src/particle.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""
particle(str::AbstractString; mass_numb=nothing, z=nothing)
Create a particle from a string representation.
# Arguments
- `str::AbstractString`: String representation of the particle
# String Format Support
- Element symbols: `"Fe"`, `"He"`
- Isotopes: `"Fe-56"`, `"D"`
- Ions: `"Fe2+"`, `"H-"`
- Common aliases: `"electron"`, `"proton"`, `"alpha"`, `"mu-"`
# Examples
```jldoctest; output = false
# Elementary particles
electron = particle("e-")
muon = particle("mu-")
positron = particle("e+")
# Ions and isotopes
proton = particle("H+")
alpha = particle("He2+")
deuteron = particle("D+")
iron56 = particle("Fe-56")
# output
Fe
```
"""
function particle(str::AbstractString; mass_numb=nothing, z=nothing)
# Check aliases first
if haskey(PARTICLE_ALIASES, str)
result = PARTICLE_ALIASES[str]
if result isa Tuple
symbol, charge, mass_number = result
return Particle(Symbol(symbol), charge, mass_number)
else
return eval(result)()
end
end

# Try to parse as element with optional mass number and charge
result = parse_particle_string(str)
if !isnothing(result)
(symbol, parsed_charge, parsed_mass_numb) = result
element = elements[symbol]
charge = determine(parsed_charge, z; default=0)
mass_number = determine(parsed_mass_numb, mass_numb; default=element.mass_number)
return Particle(symbol, charge, mass_number)
end
throw(ArgumentError("Invalid particle string format: $str"))
end

"""
particle(sym::Symbol)
Create a particle from its symbol representation.
# Examples
```jldoctest; output = false
# Elementary particles
electron = particle(:e)
muon = particle(:muon)
proton = particle(:p)
# output
H⁺
```
"""
particle(sym::Symbol; kwargs...) = particle(string(sym); kwargs...)

Particle(str::AbstractString; mass_numb=nothing, z=nothing) = particle(str; mass_numb, z)
Particle(sym::Symbol; kwargs...) = particle(string(sym); kwargs...)
66 changes: 0 additions & 66 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,72 +71,6 @@ struct Particle <: AbstractChargeParticle
end
end

"""
Particle(str::AbstractString; mass_numb=nothing, z=nothing)
Create a particle from a string representation.
# Arguments
- `str::AbstractString`: String representation of the particle
# String Format Support
- Element symbols: `"Fe"`, `"He"`
- Isotopes: `"Fe-56"`, `"D"`
- Ions: `"Fe2+"`, `"H-"`
- Common aliases: `"electron"`, `"proton"`, `"alpha"`, `"mu-"`
# Examples
```jldoctest; output = false
# Elementary particles
electron = Particle("e-")
muon = Particle("mu-")
positron = Particle("e+")
# Ions and isotopes
proton = Particle("H+")
alpha = Particle("He2+")
deuteron = Particle("D+")
iron56 = Particle("Fe-56")
# output
Fe
```
"""
function Particle(str::AbstractString; mass_numb=nothing, z=nothing)
# Check aliases first
if haskey(PARTICLE_ALIASES, str)
symbol, charge, mass_number = PARTICLE_ALIASES[str]
return Particle(Symbol(symbol), charge, mass_number)
end

# Try to parse as element with optional mass number and charge
result = parse_particle_string(str)
if !isnothing(result)
(symbol, parsed_charge, parsed_mass_numb) = result
element = elements[symbol]
charge = determine(parsed_charge, z; default=0)
mass_number = determine(parsed_mass_numb, mass_numb; default=element.mass_number)
return Particle(symbol, charge, mass_number)
end
throw(ArgumentError("Invalid particle string format: $str"))
end

"""
Particle(sym::Symbol)
Create a particle from its symbol representation.
# Examples
```jldoctest; output = false
# Elementary particles
electron = Particle(:e)
muon = Particle(:muon)
proton = Particle(:p)
# output
H⁺
```
"""
Particle(sym::Symbol; kwargs...) = Particle(string(sym); kwargs...)

"""
Particle(p::AbstractParticle)
Expand Down

0 comments on commit bac2c13

Please sign in to comment.