Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfixes related to chemical_formula #133

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/utils/chemspecies.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@

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

atomic_symbol(element::ChemicalSpecies) = element
atomic_symbol(element::ChemicalSpecies) = element

Check warning on line 202 in src/utils/chemspecies.jl

View check run for this annotation

Codecov / codecov/patch

src/utils/chemspecies.jl#L202

Added line #L202 was not covered by tests

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

Expand Down Expand Up @@ -248,11 +248,16 @@
this may be different than `atomic_symbol` for cases where `atomic_symbol`
is chosen to be more specific (i.e. designate a special atom).
"""
element_symbol(sys::AbstractSystem, index) =
element_symbol(sys::AbstractSystem, index) =

Check warning on line 251 in src/utils/chemspecies.jl

View check run for this annotation

Codecov / codecov/patch

src/utils/chemspecies.jl#L251

Added line #L251 was not covered by tests
element_symbol.(sys[index])

element_symbol(species) =
function element_symbol(species)
if atomic_number(species) == 0
:X

Check warning on line 256 in src/utils/chemspecies.jl

View check run for this annotation

Codecov / codecov/patch

src/utils/chemspecies.jl#L254-L256

Added lines #L254 - L256 were not covered by tests
else
Symbol(element(atomic_number(species)).symbol)
end
end


"""
Expand Down Expand Up @@ -296,7 +301,7 @@
"""
function atom_name(cs::ChemicalSpecies)
if cs.name == 0
return atomic_symbol(cs)
return Symbol(atomic_symbol(cs)) # atomic_symbol is a ChemicalSpecies

Check warning on line 304 in src/utils/chemspecies.jl

View check run for this annotation

Codecov / codecov/patch

src/utils/chemspecies.jl#L304

Added line #L304 was not covered by tests
else
# filter first empty space characters
as_characters = Char.( reinterpret(SVector{4, UInt8}, [cs.name])[1] )
Expand All @@ -307,4 +312,4 @@

atom_name(at) = atomic_symbol(at)

atom_name(sys::AbstractSystem, index) = atom_name.(species(sys, index))
atom_name(sys::AbstractSystem, index) = atom_name.(species(sys, index))

Check warning on line 315 in src/utils/chemspecies.jl

View check run for this annotation

Codecov / codecov/patch

src/utils/chemspecies.jl#L315

Added line #L315 was not covered by tests
5 changes: 3 additions & 2 deletions src/utils/properties.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
join(sort(parts))
end

chemical_formula(system::AbstractSystem) =
chemical_formula(element_symbol(system, :))
function chemical_formula(system::AbstractSystem)
chemical_formula(Symbol.(atomic_symbol(system, :)))

Check warning on line 21 in src/utils/properties.jl

View check run for this annotation

Codecov / codecov/patch

src/utils/properties.jl#L20-L21

Added lines #L20 - L21 were not covered by tests
end
27 changes: 13 additions & 14 deletions test/properties.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,33 @@ using UnitfulAtomic
@test chemical_formula([:Ga, :N, :O, :H, :H]) == "GaH₂NO"
end

@testset "Chemical Species" begin
@testset "Chemical Species" begin
s = ChemicalSpecies(:C)
@test atomic_number(s) == 6
@test atomic_symbol(s) == :C
s1 = ChemicalSpecies(:C; n_neutrons=7)
s2 = ChemicalSpecies(:C13)
s2 = ChemicalSpecies(:C13)
@test s1 == s2
@test atomic_number(s1) == 6
@test atomic_symbol(s1) == :C13

s3 = ChemicalSpecies(:D)
s3 = ChemicalSpecies(:D)
@test atomic_number(s3) == 1
@test element_symbol(s3) == :H
@test s3.n_neutrons == 1
end
end

@testset "Chemical formula with system" begin
lattice = tuple([12u"bohr" * rand(3) for _ in 1:3]...)
atoms = [Atom(:C13, randn(3)u"Å"),
Atom(:C14, randn(3)u"Å"),
Atom(:D, randn(3)u"Å" ),
Atom(:D, randn(3)u"Å" ),
Atom(:D, randn(3)u"Å" ),
atoms = [Atom(:C13, randn(3)u"Å"),
Atom(:C14, randn(3)u"Å"),
Atom(:D, randn(3)u"Å" ),
Atom(:D, randn(3)u"Å" ),
Atom(:D, randn(3)u"Å" ),
Atom(:X, randn(3)u"Å" ),
]
system = periodic_system(atoms, lattice)
@test species(system, :) == ChemicalSpecies.([:C13, :C14, :D, :D, :D])
@test element_symbol(system, :) == [:C, :C, :H, :H, :H]
@test chemical_formula(system) == "C₂H₃"
@test species(system, :) == ChemicalSpecies.([:C13, :C14, :D, :D, :D, :X])
@test element_symbol(system, :) == [:C, :C, :H, :H, :H, :X]
@test chemical_formula(system) == "C13C14D₃X"
end


2 changes: 1 addition & 1 deletion test/species.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ end
@test mass(ChemicalSpecies(:C)) != mass(ChemicalSpecies(:C13))
@test mass(ChemicalSpecies(:C12)) != mass(ChemicalSpecies(:C13))

@test atom_name(ChemicalSpecies(:C)) == atomic_symbol(ChemicalSpecies(:C))
@test atom_name(ChemicalSpecies(:C)) == Symbol(atomic_symbol(ChemicalSpecies(:C)))
@test atom_name(ChemicalSpecies(:C; atom_name=:MyC)) == :MyC

tmp = ChemicalSpecies(:C12; atom_name=:MyC)
Expand Down
Loading