Skip to content

Commit

Permalink
refactor: Rename ChargedParticleImpl to Particle and update related code
Browse files Browse the repository at this point in the history
The `Particle` type now represents the concrete implementation for storing particle properties.
  • Loading branch information
Beforerr committed Nov 17, 2024
1 parent 961d354 commit 0c32002
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
4 changes: 2 additions & 2 deletions docs/src/manual/particle-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ ChargedParticles.jl provides a flexible type system for representing various typ
```@raw html
<pre>
AbstractParticle
└── ChargedParticleImpl
└── Particle
</pre>
```

The package uses a simple two-level type hierarchy:
- `AbstractParticle`: Base abstract type for all particles
- `ChargedParticleImpl`: Concrete implementation storing particle properties
- `Particle`: Concrete implementation storing particle properties

## Particle Properties

Expand Down
2 changes: 1 addition & 1 deletion src/ChargedParticles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ include("./utils.jl")
include("./display.jl")
include("./_special_particles.jl")

export AbstractParticle, Particle, ChargedParticleImpl
export AbstractParticle, Particle
export mass, charge, charge_number, atomic_number, mass_number, element, mass_energy
export is_ion, is_chemical_element, is_default_isotope
export electron, proton
Expand Down
4 changes: 2 additions & 2 deletions src/properties.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ end

mass_energy(p::AbstractParticle) = _format_energy(uconvert(u"eV", p.mass * Unitful.c^2))

function Base.getproperty(p::ChargedParticleImpl, s::Symbol)
function Base.getproperty(p::Particle, s::Symbol)
s in calculated_properties && return eval(get(properties_fn_map, s, s))(p)
s in keys(synonym_properties) && return getproperty(p, synonym_properties[s])
return getfield(p, s)
end

Base.propertynames(p::ChargedParticleImpl) = (sort collect union)(keys(synonym_properties), calculated_properties, fieldnames(ChargedParticleImpl))
Base.propertynames(p::Particle) = (sort collect union)(keys(synonym_properties), calculated_properties, fieldnames(Particle))
26 changes: 17 additions & 9 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
Abstract type representing any particle in plasma physics.
See also: [`ChargedParticleImpl`](@ref)
See also: [`Particle`](@ref)
"""
abstract type AbstractParticle end

# Type for particle-like inputs
const ParticleLike = Union{AbstractParticle,Symbol,AbstractString}

"""
ChargedParticleImpl <: AbstractParticle
Particle <: AbstractParticle
Implementation type for charged particles.
Expand All @@ -23,11 +23,19 @@ Implementation type for charged particles.
# Notes
- Mass number : For elementary particles like electrons and muons, `mass_number` is 0
- Charge number : electrical charge in units of the elementary charge, usually denoted as z. https://en.wikipedia.org/wiki/Charge_number
# Examples
```jldoctest
Particle(:Fe, 2) # Creates Fe²⁺ with default mass number
# output
Fe²⁺
```
"""
@kwdef struct ChargedParticleImpl <: AbstractParticle
struct Particle <: AbstractParticle
symbol::Symbol
charge_number::Int
mass_number::Int

end

"""
Expand Down Expand Up @@ -64,7 +72,7 @@ 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 ChargedParticleImpl(Symbol(symbol), charge, mass_number)
return Particle(Symbol(symbol), charge, mass_number)
end

# Try to parse as element with optional mass number and charge
Expand All @@ -74,7 +82,7 @@ function Particle(str::AbstractString; mass_numb=nothing, z=nothing)
element = elements[symbol]
charge = determine(parsed_charge, z; default=0)
mass_number = determine(parsed_mass_numb, mass_numb; default=element.mass_number)
return ChargedParticleImpl(symbol, charge, mass_number)
return Particle(symbol, charge, mass_number)
end
throw(ArgumentError("Invalid particle string format: $str"))
end
Expand Down Expand Up @@ -112,7 +120,7 @@ Fe-54³⁺
function Particle(p::AbstractParticle; mass_numb=nothing, z=nothing)
mass_number = something(mass_numb, p.mass_number)
charge_number = something(z, p.charge_number)
ChargedParticleImpl(p.symbol, charge_number, mass_number)
Particle(p.symbol, charge_number, mass_number)
end

"""
Expand Down Expand Up @@ -143,11 +151,11 @@ See also: [`Particle(::AbstractString)`](@ref)
function Particle(atomic_number::Int; mass_numb=nothing, z=0)
element = elements[atomic_number]
mass_number = something(mass_numb, element.mass_number)
ChargedParticleImpl(element.symbol, z, mass_number)
Particle(element.symbol, z, mass_number)
end

# Convenience constructors for common particles
"""Create an electron"""
electron() = ChargedParticleImpl(:e, -1, 0)
electron() = Particle(:e, -1, 0)
"""Create a proton"""
proton() = ChargedParticleImpl(:p, 1, 1)
proton() = Particle(:p, 1, 1)
4 changes: 0 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Test
using ChargedParticles
using ChargedParticles: is_electron, is_proton
using Unitful
using Unitful: q, me

Expand Down Expand Up @@ -39,9 +38,6 @@ using Mendeleev: elements
# Test invalid particle strings
@test_throws KeyError Particle("invalid")
@test_throws KeyError Particle("Xx")

# Test invalid mass numbers
@test_throws MethodError Particle(:He, 2, -1)
end

@testset "String Representation" begin
Expand Down

0 comments on commit 0c32002

Please sign in to comment.