Skip to content

Commit

Permalink
Introduce preference for automatic ascii visualization of system (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfherbst authored Nov 21, 2024
1 parent f2d862d commit 042cde2
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 33 deletions.
1 change: 0 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ jobs:
fail-fast: false
matrix:
version:
- '1.6'
- '1.9'
- '1.10'
- 'nightly'
Expand Down
11 changes: 10 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,28 @@ version = "0.5.0"
[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
PeriodicTable = "7b2266bf-644c-5ea3-82d8-af4bbd25a884"
Preferences = "21216c6a-2e73-6563-6e65-726566657250"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
UnitfulAtomic = "a7773ee8-282e-5fa2-be4e-bd808c38a91a"

[weakdeps]
AtomsView = "ee286e10-dd2d-4ff2-afcb-0a3cd50c8041"

[extensions]
AtomsBaseAtomsViewExt = "AtomsView"

[compat]
AtomsView = "0.1"
PeriodicTable = "1"
Preferences = "1.4"
Requires = "1"
StaticArrays = "1"
Unitful = "1"
UnitfulAtomic = "1"
julia = "1.6"
julia = "1.9"

[extras]
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Expand Down
8 changes: 8 additions & 0 deletions ext/AtomsBaseAtomsViewExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module AtomsBaseAtomsViewExt
using AtomsBase
using AtomsView

function Base.show(io::IO, mime::MIME"text/html", system::AbstractSystem)
write(io, AtomsView.visualize_structure(system, mime))
end
end
14 changes: 0 additions & 14 deletions src/AtomsBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,10 @@ include("utils/visualize_ascii.jl")
include("utils/show.jl")
include("utils/atomview.jl")


# prototype implementations
include("implementation/atom.jl")
include("implementation/flexible_system.jl")
include("implementation/fast_system.jl")
include("implementation/utils.jl")


# TODO:
# - this should be converted to an extension
# - should work for AbstractSystem
function __init__()
@require AtomsView="ee286e10-dd2d-4ff2-afcb-0a3cd50c8041" begin
function Base.show(io::IO, mime::MIME"text/html", system::FlexibleSystem)
write(io, AtomsView.visualize_structure(system, mime))
end
end
end


end
4 changes: 2 additions & 2 deletions src/implementation/atom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ Supported `kwargs` include `species`, `mass`, as well as user-specific custom pr
"""
function Atom(identifier::AtomId,
position::AbstractVector{L},
velocity::AbstractVector{V} = _default_velocity(position);
species = ChemicalSpecies(identifier),
velocity::AbstractVector{V}=_default_velocity(position);
species=ChemicalSpecies(identifier),
mass::M=mass(species),
kwargs...) where {L <: Unitful.Length, V <: Unitful.Velocity, M <: Unitful.Mass}
Atom{length(position), L, V, M}(position, velocity, species,
Expand Down
45 changes: 37 additions & 8 deletions src/utils/show.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@
using Printf
using Preferences

"""
Configures the printing behaviour of `show_system`, which is invoked when a rich `text/plain`
display of an `AbstractSystem` is requested. This is for example the case in a Julia REPL.
The following options can be configured:
- `max_particles_list`: Maximal number of particles in a system until `show_system`
includes a listing of every particle. Default: 10
- `max_particles_visualize_ascii`: Maximal number of particles in a system
until `show_system` includes a representation of the system in the form of
an ascii cartoon using `visualize_ascii`. Default 0, i.e. disabled.
"""
function set_show_preferences!(; max_particles_list=nothing, max_particles_visualize_ascii=nothing)
if !isnothing(max_particles_list)
@set_preferences!("max_particles_list" => max_particles_list)
end
if !isnothing(max_particles_visualize_ascii)
@set_preferences!("max_particles_visualize_ascii" => max_particles_visualize_ascii)
end
show_preferences()
end

"""
Display the current printing behaviour of `show_system`.
See [`set_show_preferences!](@ref) for more details on the keys.
"""
function show_preferences()
(; max_particles_list=@load_preference("max_particles_list", 10),
max_particles_visualize_ascii=@load_preference("max_particles_visualize_ascii", 0))
end

"""
Suggested function to print AbstractSystem objects to screen
Expand All @@ -7,9 +38,9 @@ function show_system(io::IO, system::AbstractSystem{D}) where {D}
pbc = periodicity(system)
print(io, typeof(system).name.name, "($(chemical_formula(system))")
perstr = [p ? "T" : "F" for p in pbc]
print(io, ", pbc = ", join(perstr, ""))
print(io, ", periodicity = ", join(perstr, ""))

if !any(pbc)
if any(pbc)
box_str = ["[" * join(ustrip.(bvector), ", ") * "]"
for bvector in cell_vectors(system)]
bunit = unit(eltype(first(cell_vectors(system))))
Expand All @@ -22,11 +53,11 @@ function show_system(io::IO, ::MIME"text/plain", system::AbstractSystem{D}) wher
pbc = periodicity(system)
print(io, typeof(system).name.name, "($(chemical_formula(system))")
perstr = [p ? "T" : "F" for p in periodicity(system)]
print(io, ", pbc = ", join(perstr, ""))
print(io, ", periodicity = ", join(perstr, ""))
println(io, "):")

extra_line = false
if any(pbc)
if any(pbc)
extra_line = true
box = cell_vectors(system)
bunit = unit(eltype(first(cell_vectors(system))))
Expand All @@ -47,17 +78,15 @@ function show_system(io::IO, ::MIME"text/plain", system::AbstractSystem{D}) wher
extra_line = true
@printf io " %-17s : %s\n" string(k) string(v)
end
if length(system) < 10
if length(system) show_preferences().max_particles_list
extra_line && println(io)
for atom in system
println(io, " ", atom)
end
extra_line = true
end

# TODO We will make this configurable in a follow-up PR
show_ascii = false
if show_ascii
if length(system) show_preferences().max_particles_visualize_ascii
ascii = visualize_ascii(system)
if !isempty(ascii)
extra_line && println(io)
Expand Down
18 changes: 11 additions & 7 deletions test/printing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,23 @@ using Test
box = tuple([[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, pbc = TTT)"
# TODO: I'm not sure why the expended expression should be printed in
# this setting. Still needs to be looked at please; same below
# FlexibleSystem(CSi, pbc = TTT, cell_vectors = [[10.0, 0.0, 0.0], [0.0, 5.0, 0.0], [0.0, 0.0, 7.0]]u"a₀")"""
@test repr(flexible_system) == """FlexibleSystem(CSi, periodicity = TTT, cell_vectors = [[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, pbc = TTT)"
# FastSystem(CSi, periodic = TTT, cell_vectors = [[10.0, 0.0, 0.0], [0.0, 5.0, 0.0], [0.0, 0.0, 7.0]]u"a₀")
@test repr(fast_system) == """FastSystem(CSi, periodicity = TTT, cell_vectors = [[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

@testset "Printing atomic systems" begin
at = Atom(:Si, zeros(3) * u"m", ones(3)u"m/s"; extradata=42)
show(stdout, MIME("text/plain"), at)
box = tuple([[10, 0.0, 0.0], [0.0, 5, 0.0], [0.0, 0.0, 7]]u"bohr" ...)
flexible_system = periodic_system([at], box)
show(stdout, MIME("text/plain"), flexible_system)
end

@testset "Test ASCII representation of structures" begin
@testset "3D standard system" begin
atoms = [:Si => [0.0, -0.125, 0.0],
Expand All @@ -45,7 +49,7 @@ end
@testset "3D with negative unit cell" begin
atoms = [:Si => [0.75, 0.75, 0.75],
:Si => [0.0, 0.0, 0.0]]
box = tuple([[-2.73, -2.73, 0.0], [-2.73, 0.0, -2.73], [0.0, -2.73, -2.73]]u"Å" ...)
box = tuple([[-2.73, -2.73, 0.0], [-2.73, 0.0, -2.73], [0.0, -2.73, -2.73]]u"Å" ...)
system = periodic_system(atoms, box; fractional=true)
println(visualize_ascii(system))
end
Expand Down

2 comments on commit 042cde2

@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 register

Release notes:

This release increases the minor version and contains breaking changes:

  • bounding_box was renamed to cell_vectors everywhere for consistency. This concerns both the function AtomsBase.bounding_box as well as eventual keyword arguments to system constructors.

@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/119879

Tagging

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.5.0 -m "<description of version>" 042cde2c943a4856c9a03943d839eb61825bb6fd
git push origin v0.5.0

Please sign in to comment.