From 16c52c174c0d9409739a61178917946cd9d6b938 Mon Sep 17 00:00:00 2001 From: Christoph Ortner Date: Mon, 9 Sep 2024 11:45:29 -0700 Subject: [PATCH] Several Minor Bugfixes (#114) * bugfixes for #113, 112, 109 * some io checks for ChemicalSpecies * fixing default neutron number * add atomic_number(::Symbol) * bump v0.4.1 --------- Co-authored-by: Christoph Ortner --- Project.toml | 2 +- src/AtomsBase.jl | 2 +- src/utils/chemspecies.jl | 11 +++++++++-- test/runtests.jl | 1 + test/species.jl | 33 +++++++++++++++++++++++++++++++++ 5 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 test/species.jl diff --git a/Project.toml b/Project.toml index a89f6ac..79b0167 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "AtomsBase" uuid = "a963bdd2-2df7-4f54-a1ee-49d51e6be12a" authors = ["JuliaMolSim community"] -version = "0.4" +version = "0.4.1" [deps] LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" diff --git a/src/AtomsBase.jl b/src/AtomsBase.jl index 6785b5d..243cab1 100644 --- a/src/AtomsBase.jl +++ b/src/AtomsBase.jl @@ -5,7 +5,7 @@ using UnitfulAtomic using StaticArrays using Requires -export Atom, FlexibleSystem, FastSystem +export Atom, FlexibleSystem, FastSystem, AbstractSystem # Main Interface specification and inline docs include("interface.jl") diff --git a/src/utils/chemspecies.jl b/src/utils/chemspecies.jl index e9d1331..73c873e 100644 --- a/src/utils/chemspecies.jl +++ b/src/utils/chemspecies.jl @@ -51,7 +51,10 @@ end Base.Broadcast.broadcastable(s::ChemicalSpecies) = Ref(s) -ChemicalSpecies(z::Integer) = ChemicalSpecies(z, 0, 0) +# better to convert z -> symbol to catch special cases such as D; e.g. +# Should ChemicalSpecies(z) == ChemicalSpecies(z,z,0)? For H this is false. +ChemicalSpecies(z::Integer) = ChemicalSpecies(_chem_el_info[z].symbol) + ChemicalSpecies(sym::ChemicalSpecies) = sym ==(a::ChemicalSpecies, sym::Symbol) = @@ -79,7 +82,7 @@ for z in 1:length(_chem_el_info) end function _nneut_default(z::Integer) - nplusp = floor(Int, ustrip(u"u", _chem_el_info[z].atomic_mass)) + nplusp = round(Int, ustrip(u"u", _chem_el_info[z].atomic_mass)) return nplusp - z end @@ -135,6 +138,10 @@ end # UInt* is not readable atomic_number(element::ChemicalSpecies) = element.atomic_number +atomic_number(z::Integer) = z + +atomic_number(s::Symbol) = _sym2z[s] + atomic_symbol(element::ChemicalSpecies) = element Base.convert(::Type{Symbol}, element::ChemicalSpecies) = Symbol(element) diff --git a/test/runtests.jl b/test/runtests.jl index 1a5f0f4..bd0585e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -7,6 +7,7 @@ const GROUP_COVERAGE = !isempty(get(ENV, "GROUP_COVERAGE", "")) if GROUP == "Core" @testset "AtomsBase.jl" begin include("interface.jl") + include("species.jl") include("atom.jl") include("fast_system.jl") include("properties.jl") diff --git a/test/species.jl b/test/species.jl new file mode 100644 index 0000000..b97b9f9 --- /dev/null +++ b/test/species.jl @@ -0,0 +1,33 @@ + +using AtomsBase +using Unitful +using UnitfulAtomic +using Test + +## + +@testset "ChemicalSpecies" begin + +symbols = [:H, :He, :Li, :Be, :B, :C, :N, :O, :F, :Ne, + :Na, :Mg, :Al, :Si, :P, :S, :Cl, :Ar, :K, :Ca] + +# https://thechemicalelements.com/protons-neutrons-electrons-of-elements/ +n_neut = [0, 2, 4, 5, 6, 6, 7, 8, 10, 10, + 12, 12, 14, 14, 16, 16, 18, 22, 20, 20] + +for z = 1:10 + @test ChemicalSpecies(symbols[z]) == ChemicalSpecies(z) == ChemicalSpecies(z, n_neut[z], 0) +end + +@test ChemicalSpecies(:D) == ChemicalSpecies(1, 1, 0) +@test ChemicalSpecies(:C13) == ChemicalSpecies(6, 7, 0) + +@test atomic_number( UInt(8) ) == 8 +@test atomic_number( Int16(12) ) == 12 + +@test "$(ChemicalSpecies(:O))" == "$(ChemicalSpecies(8))" == "O" +@test "$(ChemicalSpecies(8, 8, 0))" == "O" +@test "$(ChemicalSpecies(:C; n_neutrons=6))" == "C" +@test "$(ChemicalSpecies(:C; n_neutrons=7))" == "C13" + +end