Skip to content

Commit 96495b9

Browse files
authored
Bugfixes related to chemical_formula (#133)
1 parent 1b1e77a commit 96495b9

File tree

4 files changed

+26
-21
lines changed

4 files changed

+26
-21
lines changed

src/utils/chemspecies.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ atomic_number(z::Integer) = z
199199

200200
atomic_number(s::Symbol) = _sym2z[s]
201201

202-
atomic_symbol(element::ChemicalSpecies) = element
202+
atomic_symbol(element::ChemicalSpecies) = element
203203

204204
Base.convert(::Type{Symbol}, element::ChemicalSpecies) = Symbol(element)
205205

@@ -251,8 +251,13 @@ is chosen to be more specific (i.e. designate a special atom).
251251
element_symbol(sys::AbstractSystem, index) =
252252
element_symbol.( species(sys, index) )
253253

254-
element_symbol(species) =
254+
function element_symbol(species)
255+
if atomic_number(species) == 0
256+
:X
257+
else
255258
Symbol(element(atomic_number(species)).symbol)
259+
end
260+
end
256261

257262

258263
"""
@@ -296,7 +301,7 @@ Defaults to [`atomic_symbol`](@ref), if `name` field is zero or not defined.
296301
"""
297302
function atom_name(cs::ChemicalSpecies)
298303
if cs.name == 0
299-
return atomic_symbol(cs)
304+
return Symbol(atomic_symbol(cs)) # atomic_symbol is a ChemicalSpecies
300305
else
301306
# filter first empty space characters
302307
as_characters = Char.( reinterpret(SVector{4, UInt8}, [cs.name])[1] )
@@ -307,4 +312,4 @@ end
307312

308313
atom_name(at) = atomic_symbol(at)
309314

310-
atom_name(sys::AbstractSystem, index) = atom_name.(species(sys, index))
315+
atom_name(sys::AbstractSystem, index) = atom_name.(species(sys, index))

src/utils/properties.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ function chemical_formula(symbols::AbstractVector{Symbol})
1717
join(sort(parts))
1818
end
1919

20-
chemical_formula(system::AbstractSystem) =
21-
chemical_formula(element_symbol(system, :))
20+
function chemical_formula(system::AbstractSystem)
21+
chemical_formula(Symbol.(atomic_symbol(system, :)))
22+
end

test/properties.jl

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,33 @@ using UnitfulAtomic
1111
@test chemical_formula([:Ga, :N, :O, :H, :H]) == "GaH₂NO"
1212
end
1313

14-
@testset "Chemical Species" begin
14+
@testset "Chemical Species" begin
1515
s = ChemicalSpecies(:C)
1616
@test atomic_number(s) == 6
1717
@test atomic_symbol(s) == :C
1818
s1 = ChemicalSpecies(:C; n_neutrons=7)
19-
s2 = ChemicalSpecies(:C13)
19+
s2 = ChemicalSpecies(:C13)
2020
@test s1 == s2
2121
@test atomic_number(s1) == 6
2222
@test atomic_symbol(s1) == :C13
2323

24-
s3 = ChemicalSpecies(:D)
24+
s3 = ChemicalSpecies(:D)
2525
@test atomic_number(s3) == 1
2626
@test element_symbol(s3) == :H
2727
@test s3.n_neutrons == 1
28-
end
28+
end
2929

3030
@testset "Chemical formula with system" begin
3131
lattice = tuple([12u"bohr" * rand(3) for _ in 1:3]...)
32-
atoms = [Atom(:C13, randn(3)u"Å"),
33-
Atom(:C14, randn(3)u"Å"),
34-
Atom(:D, randn(3)u"Å" ),
35-
Atom(:D, randn(3)u"Å" ),
36-
Atom(:D, randn(3)u"Å" ),
32+
atoms = [Atom(:C13, randn(3)u"Å"),
33+
Atom(:C14, randn(3)u"Å"),
34+
Atom(:D, randn(3)u"Å" ),
35+
Atom(:D, randn(3)u"Å" ),
36+
Atom(:D, randn(3)u"Å" ),
37+
Atom(:X, randn(3)u"Å" ),
3738
]
3839
system = periodic_system(atoms, lattice)
39-
@test species(system, :) == ChemicalSpecies.([:C13, :C14, :D, :D, :D])
40-
@test element_symbol(system, :) == [:C, :C, :H, :H, :H]
41-
@test chemical_formula(system) == "C₂H₃"
40+
@test species(system, :) == ChemicalSpecies.([:C13, :C14, :D, :D, :D, :X])
41+
@test element_symbol(system, :) == [:C, :C, :H, :H, :H, :X]
42+
@test chemical_formula(system) == "C13C14D₃X"
4243
end
43-
44-

test/species.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ end
6161
@test mass(ChemicalSpecies(:C)) != mass(ChemicalSpecies(:C13))
6262
@test mass(ChemicalSpecies(:C12)) != mass(ChemicalSpecies(:C13))
6363

64-
@test atom_name(ChemicalSpecies(:C)) == atomic_symbol(ChemicalSpecies(:C))
64+
@test atom_name(ChemicalSpecies(:C)) == Symbol(atomic_symbol(ChemicalSpecies(:C)))
6565
@test atom_name(ChemicalSpecies(:C; atom_name=:MyC)) == :MyC
6666

6767
tmp = ChemicalSpecies(:C12; atom_name=:MyC)

0 commit comments

Comments
 (0)