diff --git a/Project.toml b/Project.toml index cc90bc0..d75a9b3 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "AtomsBase" uuid = "a963bdd2-2df7-4f54-a1ee-49d51e6be12a" authors = ["JuliaMolSim community"] -version = "0.2.1" +version = "0.2.2" [deps] PeriodicTable = "7b2266bf-644c-5ea3-82d8-af4bbd25a884" diff --git a/docs/src/apireference.md b/docs/src/apireference.md index 289d9d7..120261c 100644 --- a/docs/src/apireference.md +++ b/docs/src/apireference.md @@ -18,6 +18,7 @@ AbstractSystem{D} atomic_mass atomic_number atomic_symbol +chemical_formula boundary_conditions bounding_box element diff --git a/src/AtomsBase.jl b/src/AtomsBase.jl index 74c7fad..a8d5918 100644 --- a/src/AtomsBase.jl +++ b/src/AtomsBase.jl @@ -5,6 +5,7 @@ import PeriodicTable: elements using StaticArrays include("interface.jl") +include("properties.jl") include("flexible_system.jl") include("atom.jl") include("atomview.jl") diff --git a/src/atom.jl b/src/atom.jl index aee04d8..0c80c51 100644 --- a/src/atom.jl +++ b/src/atom.jl @@ -58,6 +58,11 @@ function Base.convert(::Type{Atom}, id_pos::Pair{<:AtomId,<:AbstractVector{<:Uni Atom(id_pos...) end +function Base.show(io::IO, at::Atom{D, L}) where {D, L} + pos = ustrip.(at.position) + print(io, "Atom($(at.atomic_symbol), [", join(pos, ", "), "]u\"$(unit(L))\")") +end + # # Special high-level functions to construct atomic systems # diff --git a/src/flexible_system.jl b/src/flexible_system.jl index 4646d9d..beea30c 100644 --- a/src/flexible_system.jl +++ b/src/flexible_system.jl @@ -30,6 +30,11 @@ function FlexibleSystem(system::AbstractSystem; FlexibleSystem(particles, box, boundary_conditions) end +function Base.show(io::IO, system::FlexibleSystem) + print(io, "FlexibleSystem") + show_system(io, system) +end + bounding_box(sys::FlexibleSystem) = sys.box boundary_conditions(sys::FlexibleSystem) = sys.boundary_conditions species_type(sys::FlexibleSystem{D, S, L}) where {D, S, L} = S diff --git a/src/properties.jl b/src/properties.jl new file mode 100644 index 0000000..5e859cd --- /dev/null +++ b/src/properties.jl @@ -0,0 +1,37 @@ +export chemical_formula + + +""" +Returns the chemical formula of an AbstractSystem as a string. +""" +function chemical_formula(symbols::AbstractVector{Symbol}) + parts = map(collect(Set(symbols))) do sym + sym_count = count(isequal(sym), symbols) + sym_count < 2 && return string(sym) + + str_count = string(sym_count) + for i in 0:9 + str_count = replace(str_count, ('0' + i) => ('₀' + i)) # Make subscripts + end + string(sym) * str_count + end + join(sort(parts)) +end +chemical_formula(system) = chemical_formula(atomic_symbol(system)) + + +function show_system(io::IO, system::AbstractSystem{D}) where {D} + print(io, "($(chemical_formula(system)), ") + bc = boundary_conditions(system) + if all(isequal(bc[1]), bc) + print(io, typeof(bc[1]), ", ") + end + box = bounding_box(system) + if box != infinite_box(D) + box_str = ["[" * join(ustrip.(bvector), ", ") * "]" for bvector in box] + print(io, "box=[", join(box_str, ", "), "]u\"$(unit(box[1][1]))\"") + else + print(io, "box=infinite") + end + print(io, ")") +end diff --git a/test/printing.jl b/test/printing.jl new file mode 100644 index 0000000..0e9a928 --- /dev/null +++ b/test/printing.jl @@ -0,0 +1,14 @@ +using AtomsBase +using Unitful +using Test + +@testset "Printing atomic systems" begin + at = Atom(:Si, zeros(3) * u"m", extradata=42) + println(at) + + 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"Å" + system = periodic_system(atoms, box; fractional=true) + println(system) +end diff --git a/test/properties.jl b/test/properties.jl new file mode 100644 index 0000000..74a26d6 --- /dev/null +++ b/test/properties.jl @@ -0,0 +1,11 @@ +using AtomsBase +using Test + +@testset "Chemical formula" 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 + diff --git a/test/runtests.jl b/test/runtests.jl index d571164..96a45d8 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -4,4 +4,6 @@ using Test include("interface.jl") include("fast_system.jl") include("atom.jl") + include("properties.jl") + include("printing.jl") end