From bac2c1378c54518eabf79fbcfb9004aab4cbfb26 Mon Sep 17 00:00:00 2001 From: Beforerr Date: Mon, 18 Nov 2024 16:06:54 -0800 Subject: [PATCH] refactor: rename `Particle` interface for creating particles to `particle`, and add functionality to process special particles --- src/ChargedParticles.jl | 1 + src/aliases.jl | 6 ++-- src/particle.jl | 73 +++++++++++++++++++++++++++++++++++++++++ src/types.jl | 66 ------------------------------------- 4 files changed, 77 insertions(+), 69 deletions(-) create mode 100644 src/particle.jl diff --git a/src/ChargedParticles.jl b/src/ChargedParticles.jl index 6b0e3f6..12a469c 100644 --- a/src/ChargedParticles.jl +++ b/src/ChargedParticles.jl @@ -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") diff --git a/src/aliases.jl b/src/aliases.jl index 2973ef4..b16a469 100644 --- a/src/aliases.jl +++ b/src/aliases.jl @@ -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), @@ -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) \ No newline at end of file diff --git a/src/particle.jl b/src/particle.jl new file mode 100644 index 0000000..bf181d8 --- /dev/null +++ b/src/particle.jl @@ -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...) \ No newline at end of file diff --git a/src/types.jl b/src/types.jl index aa83c61..ae50846 100644 --- a/src/types.jl +++ b/src/types.jl @@ -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)