-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: more exhaustive type hierarchy (#4)
* feat: inner constructor for Particle * feat: Add CustomParticle * doc: Update particle type hierarchy to add CustomParticle * refactor: more exhaustive type hierarchy * refactor: Update special particles type hierarchy - Make _special_particles.jl public visible, add Positron * refactor: Rename ChargedParticleImpl to Particle and update related code The `Particle` type now represents the concrete implementation for storing particle properties. * refactor: rename `Particle` interface for creating particles to `particle`, and add functionality to process special particles * doc: update `particle` * chore: more aliases
- Loading branch information
Showing
16 changed files
with
277 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ makedocs( | |
], | ||
"API Reference" => "api.md" | ||
], | ||
checkdocs=:exports, | ||
doctest=true | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ Order = [:type] | |
## Constructors | ||
|
||
```@docs | ||
Particle | ||
particle | ||
proton | ||
electron | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
""" | ||
CustomParticle <: AbstractParticle | ||
A particle with user-defined mass and charge. | ||
# Fields | ||
- `mass`: The mass of the particle (can be any numeric type or Unitful quantity) | ||
- `charge_number`: Integer representing the charge state | ||
- `symbol`: Optional symbol identifier (defaults to nothing) | ||
# Examples | ||
```julia | ||
CustomParticle(1.67e-27u"kg", 1) # A particle with proton-like mass and +1 charge | ||
CustomParticle(2.0u"GeV", -1, :custom) # A custom particle with specified symbol | ||
``` | ||
""" | ||
@kwdef struct CustomParticle <: AbstractParticle | ||
mass::Unitful.Mass | ||
charge_number::Int | ||
symbol::Symbol | ||
function CustomParticle(mass, charge_number, symbol=:custom) | ||
new(mass, charge_number, symbol) | ||
end | ||
end | ||
|
||
function CustomParticle(mass_energy::Unitful.Energy, charge_number, symbol=:custom) | ||
mass = uconvert(u"kg", mass_energy / Unitful.c^2) | ||
CustomParticle(mass, charge_number, symbol) | ||
end | ||
|
||
# Override mass method for CustomParticle | ||
mass(p::CustomParticle) = p.mass | ||
# CustomParticle doesn't have a mass number | ||
mass_number(::CustomParticle) = nothing | ||
# CustomParticle doesn't have an element | ||
element(::CustomParticle) = nothing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const leptons = ("e-", "mu-", "tau-", "nu_e", "nu_mu", "nu_tau") | ||
const antileptons = ("e+", "mu+", "tau+", "anti_nu_e", "anti_nu_mu", "anti_nu_tau") | ||
const baryons = ("p+", "n") | ||
const antibaryons = ("p-", "antineutron") | ||
|
||
struct Electron <: AbstractLepton end | ||
struct Positron <: AbstractLepton end | ||
struct Muon <: AbstractLepton end | ||
struct Neutron <: AbstractFermion end | ||
|
||
# Properties | ||
atomic_number(::AbstractFermion) = 0 | ||
mass_number(::AbstractFermion) = 0 | ||
|
||
## Electron and Positron | ||
charge_number(::Electron) = -1 | ||
mass(::Electron) = me | ||
symbol(::Electron) = :e | ||
|
||
charge_number(::Positron) = 1 | ||
mass(::Positron) = me | ||
symbol(::Positron) = :e | ||
|
||
## Muon | ||
charge_number(::Muon) = -1 | ||
mass(::Muon) = 206.7682827me | ||
symbol(::Muon) = :μ | ||
|
||
## Neutron | ||
charge_number(::Neutron) = 0 | ||
mass(::Neutron) = Unitful.mn | ||
symbol(::Neutron) = :n | ||
mass_number(::Neutron) = 1 | ||
|
||
|
||
# Convenience constructors for common particles | ||
"""Create an electron""" | ||
electron() = Electron() |
Oops, something went wrong.