Skip to content

Commit

Permalink
feat: Add CustomParticle
Browse files Browse the repository at this point in the history
test: add Custom Particles test into runtest
  • Loading branch information
Beforerr committed Nov 17, 2024
1 parent 8a460f0 commit d27e04c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/ChargedParticles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ include("./properties.jl")
include("./aliases.jl")
include("./utils.jl")
include("./display.jl")
include("./custom_particles.jl")
include("./_special_particles.jl")

export AbstractParticle, Particle
export AbstractParticle, Particle, CustomParticle
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
36 changes: 36 additions & 0 deletions src/custom_particles.jl
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
15 changes: 15 additions & 0 deletions test/custom_particles.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Test, ChargedParticles, Unitful
using Unitful: q

@testset "CustomParticle" begin
# Test basic construction
p1 = CustomParticle(1.67e-27u"kg", 1)
@test mass(p1) == 1.67e-27u"kg"
@test p1.charge_number == 1
@test p1.symbol == :custom

# Test with custom symbol
p2 = CustomParticle(2.0u"GeV", -1, :my_particle)
@test mass_energy(p2) == 2.0u"GeV"
@test charge(p2) == -Unitful.q
end
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ using Mendeleev: elements
include("types.jl")
end

@testset "Custom Particles" begin
include("custom_particles.jl")
end

@testset "Properties" begin
include("properties.jl")
end
Expand Down

0 comments on commit d27e04c

Please sign in to comment.