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

Improve chemical formula #76

Merged
merged 3 commits into from
Jul 2, 2023
Merged
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
1 change: 1 addition & 0 deletions docs/src/apireference.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Pages = ["apireference.md"]
boundary_conditions
bounding_box
chemical_formula
element_symbol
isinfinite
n_dimensions
periodicity
Expand Down
18 changes: 16 additions & 2 deletions src/properties.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
export chemical_formula
export chemical_formula, element_symbol

"""
element_symbol(system)

Return the symbols corresponding to the elements of the atoms. Note that
this may be different than `atomic_symbol` for cases where `atomic_symbol`
is chosen to be more specific (i.e. designate a special atom).
"""
function element_symbol(system::AbstractSystem)
# Note that atomic_symbol cannot be used here, since this may map
# to something more specific than the element
[Symbol(element(num).symbol) for num in atomic_number(system)]
end


"""
Returns the chemical formula of an AbstractSystem as a string.
Expand All @@ -16,4 +30,4 @@ function chemical_formula(symbols::AbstractVector{Symbol})
end
join(sort(parts))
end
chemical_formula(system) = chemical_formula(atomic_symbol(system))
chemical_formula(system::AbstractSystem) = chemical_formula(element_symbol(system))
6 changes: 3 additions & 3 deletions test/printing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ using Test

atoms = [:Si => [0.0, -0.125, 0.0],
:C => [0.125, 0.0, 0.0]]
box = [[10, 0.0, 0.0], [0.0, 5, 0.0], [0.0, 0.0, 7]]u"Å"
box = [[10, 0.0, 0.0], [0.0, 5, 0.0], [0.0, 0.0, 7]]u"bohr"

flexible_system = periodic_system(atoms, box; fractional=true, data=-12)
@test repr(flexible_system) == """
FlexibleSystem(CSi, periodic = TTT, bounding_box = [[10.0, 0.0, 0.0], [0.0, 5.0, 0.0], [0.0, 0.0, 7.0]]u"Å")"""
FlexibleSystem(CSi, periodic = TTT, bounding_box = [[10.0, 0.0, 0.0], [0.0, 5.0, 0.0], [0.0, 0.0, 7.0]]u"a₀")"""
show(stdout, MIME("text/plain"), flexible_system)

fast_system = FastSystem(flexible_system)
@test repr(fast_system) == """
FastSystem(CSi, periodic = TTT, bounding_box = [[10.0, 0.0, 0.0], [0.0, 5.0, 0.0], [0.0, 0.0, 7.0]]u"Å")"""
FastSystem(CSi, periodic = TTT, bounding_box = [[10.0, 0.0, 0.0], [0.0, 5.0, 0.0], [0.0, 0.0, 7.0]]u"a₀")"""
show(stdout, MIME("text/plain"), fast_system)
show(stdout, MIME("text/plain"), fast_system[1])
end
Expand Down
16 changes: 15 additions & 1 deletion test/properties.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
using AtomsBase
using Test
using Unitful
using UnitfulAtomic

@testset "Chemical formula" begin
@testset "Chemical formula with symbols" begin
@test chemical_formula([:H]) == "H"
@test chemical_formula([:H, :H]) == "H₂"
@test chemical_formula([:H, :O, :H]) == "H₂O"
@test chemical_formula([:O, :H, :O, :H]) == "H₂O₂"
@test chemical_formula([:Ga, :N, :O, :H, :H]) == "GaH₂NO"
end

@testset "Chemical formula with system" begin
lattice = [12u"bohr" * rand(3) for _ in 1:3]
atoms = [Atom(6, randn(3)u"Å"; atomic_symbol=:C1),
Atom(6, randn(3)u"Å"; atomic_symbol=:C2),
Atom(1, randn(3)u"Å"; atomic_symbol=:D),
Atom(1, randn(3)u"Å"; atomic_symbol=:D),
Atom(1, randn(3)u"Å"; atomic_symbol=:D),
]
system = periodic_system(atoms, lattice)
@test atomic_symbol(system) == [:C1, :C2, :D, :D, :D]
@test chemical_formula(system) == "C₂H₃"
end