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

Basic printing for atomic structures #39

Merged
merged 2 commits into from
Mar 22, 2022
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
1 change: 1 addition & 0 deletions docs/src/apireference.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ AbstractSystem{D}
atomic_mass
atomic_number
atomic_symbol
chemical_formula
boundary_conditions
bounding_box
element
Expand Down
1 change: 1 addition & 0 deletions src/AtomsBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
5 changes: 5 additions & 0 deletions src/atom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down
5 changes: 5 additions & 0 deletions src/flexible_system.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions src/properties.jl
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions test/printing.jl
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions test/properties.jl
Original file line number Diff line number Diff line change
@@ -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

2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ using Test
include("interface.jl")
include("fast_system.jl")
include("atom.jl")
include("properties.jl")
include("printing.jl")
end