From 0c32002248dee05b0d63d4735fa04484de168db6 Mon Sep 17 00:00:00 2001 From: Beforerr Date: Sat, 16 Nov 2024 15:25:49 -0800 Subject: [PATCH] refactor: Rename ChargedParticleImpl to Particle and update related code The `Particle` type now represents the concrete implementation for storing particle properties. --- docs/src/manual/particle-types.md | 4 ++-- src/ChargedParticles.jl | 2 +- src/properties.jl | 4 ++-- src/types.jl | 26 +++++++++++++++++--------- test/runtests.jl | 4 ---- 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/docs/src/manual/particle-types.md b/docs/src/manual/particle-types.md index 0f7fbb7..70d098d 100644 --- a/docs/src/manual/particle-types.md +++ b/docs/src/manual/particle-types.md @@ -7,13 +7,13 @@ ChargedParticles.jl provides a flexible type system for representing various typ ```@raw html
 AbstractParticle
-└── ChargedParticleImpl
+└── Particle
 
``` 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 diff --git a/src/ChargedParticles.jl b/src/ChargedParticles.jl index 67af0d3..0a8f491 100644 --- a/src/ChargedParticles.jl +++ b/src/ChargedParticles.jl @@ -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 diff --git a/src/properties.jl b/src/properties.jl index 6029e92..5483a42 100644 --- a/src/properties.jl +++ b/src/properties.jl @@ -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)) diff --git a/src/types.jl b/src/types.jl index 663d51c..a326ff9 100644 --- a/src/types.jl +++ b/src/types.jl @@ -3,7 +3,7 @@ Abstract type representing any particle in plasma physics. -See also: [`ChargedParticleImpl`](@ref) +See also: [`Particle`](@ref) """ abstract type AbstractParticle end @@ -11,7 +11,7 @@ abstract type AbstractParticle end const ParticleLike = Union{AbstractParticle,Symbol,AbstractString} """ - ChargedParticleImpl <: AbstractParticle + Particle <: AbstractParticle Implementation type for charged particles. @@ -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 """ @@ -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 @@ -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 @@ -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 """ @@ -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) \ No newline at end of file +proton() = Particle(:p, 1, 1) \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index 2724437..c326b5b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,6 +1,5 @@ using Test using ChargedParticles -using ChargedParticles: is_electron, is_proton using Unitful using Unitful: q, me @@ -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