Skip to content

Commit

Permalink
Basic printing for atomic structures (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfherbst authored Mar 22, 2022
1 parent b7a5a19 commit f9ec6af
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 1 deletion.
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

2 comments on commit f9ec6af

@mfherbst
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/57083

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.2 -m "<description of version>" f9ec6affd5a7124065e870987fd4489fea40f5da
git push origin v0.2.2

Please sign in to comment.