Skip to content

Commit

Permalink
Merge pull request #57 from tjjarvinen/ABv0.5_update
Browse files Browse the repository at this point in the history
update to AtomsBase v0.5
  • Loading branch information
jameskermode authored Nov 28, 2024
2 parents 664416c + 53c2135 commit 5df541d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ UnitfulAtomic = "a7773ee8-282e-5fa2-be4e-bd808c38a91a"
extxyz_jll = "6ecdc6fc-93a8-5528-aee3-ac7ae1c60be7"

[compat]
AtomsBase = "0.4.1"
AtomsBase = "0.5"
PeriodicTable = "1"
StaticArrays = "1.5"
Unitful = "1"
Expand Down
18 changes: 9 additions & 9 deletions src/atoms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ function Atoms(system::AbstractSystem{D})
end

system_data = Dict{Symbol,Any}(
:bounding_box => bounding_box(system),
:cell_vectors => cell_vectors(system),
:periodicity => periodicity(system)
)

# Extract extra system properties
for (k, v) in pairs(system)
atoms_base_keys = (:charge, :multiplicity, :periodicity, :bounding_box)
atoms_base_keys = (:charge, :multiplicity, :periodicity, :cell_vectors)
if k in atoms_base_keys || v isa ExtxyzType || v isa AbstractArray{<: ExtxyzType}
# These are either Unitful quantities, which are uniformly supported
# across all of AtomsBase or the value has a type that Extxyz can write
Expand Down Expand Up @@ -149,7 +149,7 @@ function Atoms(dict::Dict{String, Any})

system_data = Dict{Symbol, Any}()
if haskey(dict, "cell")
system_data[:bounding_box] = collect(eachrow(dict["cell"]))u"Å"
system_data[:cell_vectors] = collect(eachrow(dict["cell"]))u"Å"
if haskey(dict, "pbc")
system_data[:periodicity] = tuple(dict["pbc"]...)
else
Expand All @@ -159,7 +159,7 @@ function Atoms(dict::Dict{String, Any})
else # Infinite system
haskey(dict, "pbc") && @warn "'pbc' ignored since no 'cell' entry found in dict."
system_data[:periodicity] = (false, false, false)
system_data[:bounding_box] = ( SVector(Inf, 0.0, 0.0) * u"Å",
system_data[:cell_vectors] = ( SVector(Inf, 0.0, 0.0) * u"Å",
SVector(0.0, Inf, 0.0) * u"Å",
SVector(0.0, 0.0, Inf) * u"Å" )
end
Expand Down Expand Up @@ -217,14 +217,14 @@ function write_dict(atoms::Atoms)

pbc = atoms.system_data.periodicity
cell = zeros(D, D)
for (i, bvector) in enumerate(atoms.system_data.bounding_box)
for (i, bvector) in enumerate(atoms.system_data.cell_vectors)
cell[i, :] = ustrip.(u"Å", bvector)
end

# Deal with other system keys
info = Dict{String,Any}()
for (k, v) in pairs(atoms.system_data)
k in (:periodicity, :bounding_box) && continue # Already dealt with
k in (:periodicity, :cell_vectors) && continue # Already dealt with
if k in (:charge, )
info[string(k)] = ustrip(u"e_au", atoms.system_data[k])
elseif v isa ExtxyzType
Expand Down Expand Up @@ -253,14 +253,14 @@ write_dict(system::AbstractSystem{D}) = write_dict(Atoms(system))

Base.length(sys::Atoms) = length(sys.atom_data.position)
Base.size(sys::Atoms) = (length(sys), )
AtomsBase.bounding_box(sys::Atoms) = sys.system_data.bounding_box
AtomsBase.cell_vectors(sys::Atoms) = sys.system_data.cell_vectors
AtomsBase.periodicity(sys::Atoms) = sys.system_data.periodicity

# AtomsBase now requires a cell object to be returned instead of bounding_box
# and boundary conditions. But this can just be constructed on the fly.
AtomsBase.cell(sys::Atoms) = AtomsBase.PeriodicCell(;
cell_vectors = sys.system_data.bounding_box,
periodicity = sys.system_data.periodicity )
cell_vectors = cell_vectors(sys),
periodicity = periodicity(sys) )

Base.getindex(sys::Atoms, x::Symbol) = getindex(sys.system_data, x)
Base.haskey(sys::Atoms, x::Symbol) = haskey(sys.system_data, x)
Expand Down
2 changes: 1 addition & 1 deletion test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[compat]
AtomsBaseTesting = "0.2"
AtomsBaseTesting = "0.4"

[deps]
AtomsBase = "a963bdd2-2df7-4f54-a1ee-49d51e6be12a"
Expand Down
19 changes: 12 additions & 7 deletions test/atomsbase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function simple_test_approx_eq(sys1, sys2; test_cell = true)
@test atomic_number(sys1, :) == atomic_number(sys2, :)
@test atomic_symbol(sys1, :) == atomic_symbol(sys2, :)
@test periodicity(sys1) == periodicity(sys2)
@test all(bounding_box(sys1) .≈ bounding_box(sys2))
@test all(cell_vectors(sys1) .≈ cell_vectors(sys2))
if test_cell
@test cell(sys1) == cell(sys2)
end
Expand All @@ -27,7 +27,13 @@ end
end

@testset "Conversion AtomsBase -> dict (velocity)" begin
system, atoms, atprop, sysprop, box, bcs = make_test_system()
tmp = make_test_system()
system = tmp.system
atoms = tmp.atoms
atprop = tmp.atprop
sysprop = tmp.sysprop
box = tmp.cell.cell_vectors
bcs = tmp.cell.periodicity
atoms = ExtXYZ.write_dict(Atoms(system))

c3ll = zeros(3, 3)
Expand All @@ -47,16 +53,16 @@ end
@test info["multiplicity"] == sysprop.multiplicity

arrays = atoms["arrays"]
@test arrays["Z"] == AtomsBase.atomic_number.(atprop.atomic_symbol)
@test arrays["species"] == string.(atprop.atomic_symbol)
@test arrays["Z"] == AtomsBase.atomic_number.(atprop.species)
@test arrays["species"] == string.(atprop.species)
# mass is not written to the dict because the mass == mass(element)
# @test arrays["mass"] == ustrip.(u"u", atprop.atomic_mass)
@test arrays["pos"] ustrip.(u"Å", hcat(atprop.position...)) atol=1e-10
@test arrays["velocities"] ustrip.(sqrt(u"eV"/u"u"),
hcat(atprop.velocity...)) atol=1e-10

expected_atkeys = ["Z", "species", "charge", "covalent_radius",
"magnetic_moment", "pos", "vdw_radius", "velocities"]
"magnetic_moment", "mass", "pos", "vdw_radius", "velocities"]
@test sort(collect(keys(arrays))) == sort(expected_atkeys)
@test arrays["magnetic_moment"] == atprop.magnetic_moment
@test arrays["vdw_radius"] == ustrip.(u"Å", atprop.vdw_radius)
Expand Down Expand Up @@ -89,7 +95,6 @@ end
atoms = @test_logs((:warn, r"Unitful quantity massdata is not yet"),
(:warn, r"Writing quantities of type Symbol"),
(:warn, r"Unitful quantity md is not yet"),
(:warn, r"Mismatch between atomic numbers and atomic symbols"),
match_mode=:any, ExtXYZ.write_dict(Atoms(system)))

@test atoms["arrays"]["species"] == ["H", "H", "C", "N", "He"]
Expand Down Expand Up @@ -156,7 +161,7 @@ end
end

atoms = ExtXYZ.load(fname)
box = bounding_box(atoms)
box = cell_vectors(atoms)
@test [box[dim][dim] for dim=1:3] == [Inf * u"bohr" for dim=1:3]
finally
isfile(fname) && rm(fname)
Expand Down

0 comments on commit 5df541d

Please sign in to comment.