Skip to content

Commit

Permalink
feat: inner constructor for Particle
Browse files Browse the repository at this point in the history
  • Loading branch information
Beforerr committed Nov 17, 2024
1 parent 0c32002 commit 8a460f0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
1 change: 0 additions & 1 deletion docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ Order = [:type]
## Constructors

```@docs
Particle
proton
electron
```
Expand Down
8 changes: 4 additions & 4 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ p = proton() # proton
α = Particle("alpha") # alpha particle
# Create ions and isotopes
fe3 = Particle("Fe3+") # Iron(III) ion
fe56 = Particle("Fe-56") # Iron-56
fe = Particle("Fe-56 3+") # Iron(III) ion
fe54 = Particle(:Fe, 3, 54)
d = Particle("D+") # Deuteron
# Access properties
println("Electron mass: ", mass(e))
println("Alpha particle charge: ", charge(α))
println("Iron charge: ", charge(fe3))
println("Iron-56 mass number: ", mass_number(fe56))
println("Iron(III) ion charge: ", charge(fe))
println("Iron-54 mass number: ", mass_number(fe54))
println("Deuteron mass: ", mass(d))
```

Expand Down
11 changes: 9 additions & 2 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Implementation type for charged particles.
# Fields
- `symbol::Symbol`: Chemical symbol or particle identifier (e.g., :Fe, :e, :μ)
- `charge_number::Int`: Number of elementary charges (can be negative)
- `mass_number::Int`: Total number of nucleons (protons + neutrons)
- `mass_number::Int`: Total number of nucleons (protons + neutrons). If not provided, defaults to the most common isotope mass number
# Notes
- Mass number : For elementary particles like electrons and muons, `mass_number` is 0
Expand All @@ -35,7 +35,14 @@ struct Particle <: AbstractParticle
symbol::Symbol
charge_number::Int
mass_number::Int

function Particle(symbol, charge_number, mass_number=nothing)
if isnothing(mass_number)
mass_number = elements[symbol].mass_number
else
mass_number >= 0 || throw(ArgumentError("Mass number must be non-negative, got $mass_number"))
end
new(symbol, charge_number, mass_number)
end
end

"""
Expand Down
9 changes: 9 additions & 0 deletions test/types.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
using Test, ChargedParticles, Unitful
using ChargedParticles: is_electron, is_proton
using Unitful: q

@testset "Constructor" begin
# Test invalid mass numbers
@test_throws ArgumentError Particle(:He, 2, -1)

# Default mass number
@test mass_number(Particle(:He, 2)) == 4
end

@testset "String Constructor" begin
# Test common aliases
@test is_electron(Particle("electron"))
Expand Down

0 comments on commit 8a460f0

Please sign in to comment.