Skip to content

Commit

Permalink
fix printing of structures
Browse files Browse the repository at this point in the history
  • Loading branch information
ACEsuit committed May 22, 2024
1 parent 6520fdb commit 64c1c7f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/DecoratedParticles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module DecoratedParticles
# core of the package : manipulating particle states
include("states.jl")

include("show.jl")

include("temp/temp.jl")
include("atomsbase.jl")

include("show.jl")

end
23 changes: 20 additions & 3 deletions src/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ function showdigits!(n::Integer)
end

_2str(x) = string(x)
_2str(x::AbstractFloat) = "[$(round(x, digits=_showdigits[]))]"
_2str(x::Complex) = "[$(round(x, digits=_showdigits[]))]"

_2str(x::AbstractFloat) = "$(round(x, digits=_showdigits[]))"
_2str(x::Complex) = "$(round(x, digits=_showdigits[]))"
_2str(x::Quantity) = _2str(ustrip(x)) * " $(unit(x))"

_2str(x::SVector{N, <: AbstractFloat}) where {N} = string(round.(x, digits=_showdigits[]))
_2str(x::SVector{N, <: Complex}) where {N} = string(round.(x, digits=_showdigits[]))[11:end]
_2str(x::SVector{N, <: Quantity}) where {N} = _2str(ustrip.(x)) * " $(unit(x[1]))"
Expand All @@ -37,11 +40,25 @@ _leftbra( X::VState) = "⦅"
_rightbra(X::VState) = ""

function Base.show(io::IO, X::XState)
_str(sym) = "$(sym):$(_2str(getproperty(_x(X), sym)))"
_str(sym) = "$(sym)=$(_2str(getproperty(_x(X), sym)))"
strs = [ _str(sym) for sym in keys(_x(X)) ]
str = prod(strs[i] * ", " for i = 1:length(strs)-1; init="") * strs[end]
# str = prod( "$(sym):$(_2str(getproperty(_x(X), sym))), "
# for sym in keys(_x(X)) )
print(io, _leftbra(X) * str * _rightbra(X))
end


function Base.show(io::IO, sys::Union{AosSystem, SoaSystem})
println(io, typeof(sys).name.name, ": len = $(length(sys)), ")
print(io, " ", get_cell(sys))
for i = 1:min(4, length(sys))
println(io, " ", sys[i])
end
if length(sys) > 4
println(io, " ... $(length(sys)-4) particles not shown ...")
end
end

Base.show(io::IO, ::MIME"text/plain", sys::Union{AosSystem, SoaSystem}) =
show(io, sys)
8 changes: 4 additions & 4 deletions test/test_atomsbase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ display(x)
# convert an entire system

sys = rattle!(bulk(:Si, cubic=true) * 2, 0.1);
aos = DP.AosSystem(sys);
soa = DP.SoaSystem(sys);
aos = DP.AosSystem(sys)
soa = DP.SoaSystem(sys)

aos[1]
soa[1]
Expand All @@ -54,8 +54,8 @@ end
# some performance related tests

sys = rattle!(bulk(:Si, cubic=true) * 2, 0.1);
aos = DP.AosSystem(sys);
soa = DP.SoaSystem(sys);
aos = DP.AosSystem(sys)
soa = DP.SoaSystem(sys)

x1 = aos[1]
x2 = soa[1]
Expand Down

0 comments on commit 64c1c7f

Please sign in to comment.