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

Fix a bug in AtomsBase interface #38

Merged
merged 1 commit into from
Sep 20, 2023
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
4 changes: 4 additions & 0 deletions src/atoms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ function Atoms(dict::Dict{String, Any})
atom_data[Symbol(key)] = arrays[key] * u"Å"
elseif key in ("charge", ) # Add charge unit
atom_data[Symbol(key)] = arrays[key] * u"e_au"
elseif typeof(arrays[key]) <: AbstractMatrix
atom_data[Symbol(key)] = [ collect(col) for col in eachcol(arrays[key]) ]
else
atom_data[Symbol(key)] = arrays[key]
end
Expand Down Expand Up @@ -198,6 +200,8 @@ function write_dict(atoms::Atoms)
arrays[string(k)] = ustrip.(u"e_au", v)
elseif v isa AbstractVector{<:ExtxyzType}
arrays[string(k)] = v # These can be written losslessly
elseif v isa AbstractArray && eltype(v) <: AbstractVector{<:ExtxyzType}
arrays[string(k)] = reduce(hcat, v)
else
@warn "Writing quantities of type $(typeof(v)) is not supported in write_dict."
end
Expand Down
25 changes: 25 additions & 0 deletions test/atomsbase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,28 @@ end
end
test_approx_eq(system, io_system; rtol=1e-4)
end

@testset "Extra variables for atoms" begin
text = """2
Lattice="2.614036117884091 0.0 0.0 0.0 2.6528336296738044 0.0 0.0 0.0 3.8250280122051756" Properties=species:S:1:pos:R:3:force:R:3:tags:I:1 config_type=FLD_TiAl spacegroup="P 1" virial="5.072173561696366 0.1220123768779895 0.6518229755809941 0.1220123768779895 4.667636799854875 0.5969893898844183 0.6518229755809941 0.5969893898844183 4.700422750506493" energy=-1703.64063822 unit_cell=conventional n_minim_iter=2 pbc="T T T"
Ti 1.30924260 1.32316179 1.62637131 0.86219000 0.78737000 2.65969000 0
Al 0.11095015 0.09471147 -0.05013464 -0.86219000 -0.78737000 -2.65969000 1
"""
infile = tempname()
outfile = tempname()
open(infile, "w") do io
print(io, text)
end
frame = read_frame(infile)
atoms = Atoms(frame)
@test all( atoms[1][:force] .== [0.86219000, 0.78737000, 2.65969000] )
@test all( atoms[2][:force] .== [-0.86219000, -0.78737000, -2.65969000] )
@test atoms[1][:tags] == 0
@test atoms[2][:tags] == 1
ExtXYZ.save(outfile, atoms)
new_atoms = ExtXYZ.load(outfile)
@test all( atoms[1][:force] .== new_atoms[1][:force] )
@test all( atoms[2][:force] .== new_atoms[2][:force] )
@test atoms[1][:tags] == new_atoms[1][:tags]
@test atoms[2][:tags] == new_atoms[2][:tags]
end
Loading