Skip to content

Commit

Permalink
Merge pull request #25 from MineralsCloud:AtomsBaseExt
Browse files Browse the repository at this point in the history
Implement `AtomsBaseExt`
  • Loading branch information
singularitti authored Oct 18, 2023
2 parents e951293 + 59730d4 commit 7f93738
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
StructEquality = "6ec83bb0-ed9f-11e9-3b4c-2b04cb4e219c"

[weakdeps]
AtomsBase = "a963bdd2-2df7-4f54-a1ee-49d51e6be12a"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
UnitfulLinearAlgebra = "c14bd059-d406-4571-8f61-9bd20e53c30b"

[extensions]
AtomsBaseExt = "AtomsBase"
UnitfulExt = "Unitful"
UnitfulLinearAlgebraExt = ["Unitful", "UnitfulLinearAlgebra"]

Expand All @@ -22,9 +24,11 @@ StructEquality = "1, 2"
julia = "1"

[extras]
AtomsBase = "a963bdd2-2df7-4f54-a1ee-49d51e6be12a"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
UnitfulAtomic = "a7773ee8-282e-5fa2-be4e-bd808c38a91a"
UnitfulLinearAlgebra = "c14bd059-d406-4571-8f61-9bd20e53c30b"

[targets]
test = ["Test", "Unitful", "UnitfulAtomic"]
test = ["Test", "AtomsBase", "Unitful", "UnitfulAtomic", "UnitfulLinearAlgebra"]
38 changes: 38 additions & 0 deletions ext/AtomsBaseExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module AtomsBaseExt

using AtomsBase:
AbstractSystem,
Atom,
bounding_box,
periodicity,
isinfinite,
element_symbol,
periodic_system
using CrystallographyCore: basisvectors, eachatom

import AtomsBase: FlexibleSystem
import CrystallographyCore: Lattice, Cell

Lattice(system::AbstractSystem) = Lattice(bounding_box(system))

function Cell(system::AbstractSystem)
if !all(periodicity(system)) || isinfinite(system)
error("the system is not periodic!")
end
lattice = Lattice(system)
atoms = element_symbol(system)
positions = position(system)
reduced = inv(lattice).(positions)
return Cell(lattice, reduced, atoms)
end

function FlexibleSystem(cell::Cell)
lattice = Lattice(cell)
box = collect(basisvectors(lattice))
atomicpositions = map(eachatom(cell)) do (atom, position)
Atom(atom, lattice(position))
end
return periodic_system(atomicpositions, box; fractional=true)
end

end
20 changes: 20 additions & 0 deletions test/AtomsBase.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using AtomsBase
using Unitful: ustrip, @u_str
using UnitfulLinearAlgebra

# See https://juliamolsim.github.io/AtomsBase.jl/stable/tutorial/#System-interface-and-conventions v0.3.5
@testset "Test example from AtomsBase.jl documentation" begin
box = [[10.0, 0.0, 0.0], [0.0, 10.0, 0.0], [0.0, 0.0, 10.0]]u"Å" # Note the unit!
bc = [Periodic(), Periodic(), Periodic()]
hydrogen = FlexibleSystem(
[Atom(:H, [0, 0, 1.0]u"bohr"), Atom(:H, [0, 0, 3.0]u"bohr")], box, bc
)
cell = Cell(
box, [[0, 0, ustrip(u"Å", 0.1u"bohr")], [0, 0, ustrip(u"Å", 0.3u"bohr")]], [:H, :H]
)
@test Cell(hydrogen) == cell
@test all(
position(FlexibleSystem(cell)) .≈
[uconvert.(u"Å", [0, 0, 1.0]u"bohr"), uconvert.(u"Å", [0, 0, 3.0]u"bohr")],
)
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ using Test
# Write your tests here.
include("lattice.jl")
include("eachatom.jl")
include("AtomsBase.jl")
end

0 comments on commit 7f93738

Please sign in to comment.