Skip to content

Commit

Permalink
perf: 10 times faster mass function (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
Beforerr authored Nov 24, 2024
1 parent ee9f77b commit 48900da
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
20 changes: 20 additions & 0 deletions benchmarks/runbencmarks.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Chairmarks
using ChargedParticles

p = Particle("Fe 2+")
sp = SParticle(2, 26, 56)

@b mass(p)
@b mass(sp)

@b charge(p)
@b charge(sp)

@b mass_number(p)
@b mass_number(sp)

@b charge_number(p)
@b charge_number(sp)

@b element(p)
@b element(sp)
13 changes: 9 additions & 4 deletions src/properties.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Mendeleev: isotopes_data, Isotopes, ChemElem

const calculated_properties = (:charge_number, :charge, :atomic_number, :element, :mass_energy, :mass, :symbol)
const properties_fn_map = Dict()
const synonym_properties = Dict(
Expand All @@ -9,21 +11,24 @@ const synonym_properties = Dict(
)

"""Retrieve the mass of an element isotope."""
function mass(element, mass_number)
for iso in element.isotopes
function mass(isotopes::Isotopes, mass_number)
for iso in isotopes
if iso.mass_number == mass_number
return iso.mass
end
end
throw(ArgumentError("No isotope found with mass number $mass_number for element $element"))
end

mass(element::ChemElem, mass_number) = mass(element.isotopes, mass_number)
mass(atomic_number::Integer, mass_number) = mass(isotopes_data[atomic_number], mass_number)


# Basic properties
"""Return the mass of the particle"""
function mass(p::AbstractParticle)
base_mass = mass(p.element, p.mass_number)
return base_mass - p.charge_number * Unitful.me
base_mass = mass(atomic_number(p), mass_number(p))
return base_mass - charge_number(p) * Unitful.me
end

charge_number(p::AbstractParticle) = p.charge_number
Expand Down

0 comments on commit 48900da

Please sign in to comment.