-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for (A)XSF XCrySDen file format (#22)
* Shamelessly copy from extxyz interface for xsf interface * Fix typo in filename * Rename XSF to XCrySDenStructureFormat
- Loading branch information
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import XCrySDenStructureFormat as XSF | ||
|
||
""" | ||
Parse or write file using [XSF](https://github.com/azadoks/XCrySDenStructureFormat.jl) | ||
Supported formats: | ||
- [XSF](http://www.xcrysden.org/doc/XSF.html) | ||
""" | ||
struct XsfParser <: AbstractParser end | ||
|
||
function supports_parsing(::XsfParser, file; save, trajectory) | ||
_, ext = splitext(file) | ||
ext in (".xsf", ".axsf") | ||
end | ||
|
||
function load_system(::XsfParser, file::AbstractString, index=nothing) | ||
if isnothing(index) | ||
frames = XSF.load_xsf(file) | ||
isempty(frames) && error( | ||
"XSF returned no frames. Check the passed file is a valid (a)xsf file." | ||
) | ||
return last(frames) | ||
else | ||
return XSF.load_xsf(file)[index] | ||
end | ||
end | ||
|
||
function save_system(::XsfParser, file::AbstractString, system::AbstractSystem) | ||
XSF.save_xsf(file, system) | ||
end | ||
|
||
function load_trajectory(::XsfParser, file::AbstractString) | ||
XSF.load_xsf(file) | ||
end | ||
|
||
function save_system(::XsfParser, file::AbstractString, system::AbstractSystem) | ||
XSF.save_xsf(file, system) | ||
end | ||
|
||
function save_trajectory(::XsfParser, file::AbstractString, | ||
systems::AbstractVector{<:AbstractSystem}) | ||
XSF.save_xsf(file, systems) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using AtomsIO | ||
using Test | ||
using AtomsBaseTesting | ||
|
||
@testset "XSF system write / read" begin | ||
system = make_test_system().system | ||
mktempdir() do d | ||
outfile = joinpath(d, "output.xsf") | ||
save_system(XsfParser(), outfile, system) | ||
system2 = load_system(XsfParser(), outfile) | ||
test_approx_eq(system, system2; rtol=1e-6) | ||
end | ||
end | ||
|
||
@testset "XSF trajectory write/read" begin | ||
systems = [make_test_system().system for _ in 1:3] | ||
mktempdir() do d | ||
outfile = joinpath(d, "output.axsf") | ||
save_trajectory(XsfParser(), outfile, systems) | ||
newsystems = load_trajectory(XsfParser(), outfile) | ||
for (system, newsystem) in zip(systems, newsystems) | ||
test_approx_eq(system, newsystem; rtol=1e-6) | ||
end | ||
test_approx_eq(systems[end], load_system(XsfParser(), outfile); rtol=1e-6) | ||
test_approx_eq(systems[2], load_system(XsfParser(), outfile, 2); rtol=1e-6) | ||
end | ||
end | ||
|
||
@testset "ExtXYZ supports_parsing" begin | ||
import AtomsIO: supports_parsing | ||
prefix = "test" | ||
save = trajectory = true | ||
@test supports_parsing(XsfParser(), prefix * ".xsf"; save, trajectory) | ||
@test supports_parsing(XsfParser(), prefix * ".axsf"; save, trajectory) | ||
@test !supports_parsing(XsfParser(), prefix * ".bxsf"; save, trajectory) | ||
@test !supports_parsing(XsfParser(), prefix * ".cif"; save, trajectory) | ||
end |