-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sketching out an atomsbase interface
- Loading branch information
ACEsuit
committed
May 22, 2024
1 parent
046c4bb
commit a34d094
Showing
4 changed files
with
114 additions
and
2 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 |
---|---|---|
|
@@ -5,5 +5,6 @@ include("states.jl") | |
|
||
include("show.jl") | ||
|
||
include("structures.jl") | ||
|
||
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 |
---|---|---|
@@ -1 +1,68 @@ | ||
|
||
import AtomsBase | ||
import AtomsBase: AbstractSystem, ChemicalElement, | ||
position, velocity, atomic_mass, atomic_number, | ||
atomic_symbol | ||
|
||
# --------------------------------------------------- | ||
# an `Atom` is now just a `PState`, so we define | ||
# accessors for the PState fields with canonical names. | ||
|
||
symbol(::typeof(position)) = :𝐫 | ||
symbol(::typeof(velocity)) = :𝐯 | ||
symbol(::typeof(atomic_mass)) = :m | ||
symbol(::typeof(atomic_symbol)) = :Z | ||
|
||
position(atom::PState) = atom.𝐫 | ||
velocity(atom::PState) = atom.𝐯 | ||
atomic_mass(atom::PState) = atom.m | ||
atomic_symbol(atom::PState) = atom.Z # this one I'm not sure about | ||
|
||
atomic_number(atom::PState) = atomic_number(atomic_symbol(atom)) | ||
|
||
""" | ||
Generate an atom with the given properties. | ||
""" | ||
atom(at; properties = (position, atomic_mass, atomic_symbol)) = | ||
PState((; [symbol(p) => p(at) for p in properties]...)) | ||
|
||
# --------------------------------------------------- | ||
|
||
mutable struct AosSystem{D, TCELL, TPART} <: AbstractSystem{D} | ||
cell::TCELL | ||
particles::Vector{TPART} | ||
# -------- | ||
meta::Dict{String, Any} | ||
end | ||
|
||
|
||
function AosSystem(sys::AbstractSystem; | ||
properties = (position, atomic_mass, atomic_symbol), ) | ||
|
||
X = [ atom(sys[i]; properties = properties) for i = 1:length(sys) ] | ||
cell = AtomsBase.get_cell(sys) | ||
D = AtomsBase.n_dimensions(cell) | ||
return AosSystem{D, typeof(cell), eltype(X)}(cell, X, Dict{String, Any}()) | ||
end | ||
|
||
|
||
# --------------------------------------------------- | ||
# implementing the interface | ||
|
||
Base.length(at::AosSystem) = length(at.particles) | ||
|
||
Base.getindex(at::AosSystem, i::Int) = at.particles[i] | ||
|
||
for f in (:position, :velocity, :atomic_mass, :atomic_symbol) | ||
@eval $f(sys::AosSystem) = [ $f(x) for x in sys.particles ] | ||
@eval $f(sys::AosSystem, i::Integer) = $f(sys.particles[i]) | ||
@eval $f(sys::AosSystem, inds::AbstractVector) = [$f(sys.particles[i]) for i in inds] | ||
end | ||
|
||
AtomsBase.get_cell(at::AosSystem) = at.cell | ||
|
||
AtomsBase.n_dimensions(at::AosSystem) = AtomsBase.n_dimensions(at.cell) | ||
AtomsBase.bounding_box(at::AosSystem) = AtomsBase.bounding_box(at.cell) | ||
AtomsBase.boundary_conditions(at::AosSystem) = AtomsBase.boundary_conditions(at.cell) | ||
AtomsBase.periodicity(at::AosSystem) = AtomsBase.periodicity(at.cell) | ||
|
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,41 @@ | ||
|
||
using DecoratedParticles, AtomsBase, StaticArrays, Unitful, Test | ||
using AtomsBase: ChemicalElement, Atom | ||
using AtomsBuilder: bulk, rattle! | ||
DP = DecoratedParticles | ||
|
||
## | ||
#generate an atom and check that the accessors work | ||
|
||
x = PState(𝐫 = SA[1.0, 2.0, 3.0], 𝐯 = SA[0.1, 0.2, 0.3], | ||
m = 1.0, Z = ChemicalElement(6) ) | ||
display(x) | ||
@test position(x) == x.𝐫 | ||
@test velocity(x) == x.𝐯 | ||
@test atomic_mass(x) == x.m | ||
@test atomic_symbol(x) == x.Z | ||
@test atomic_number(x) == 6 | ||
|
||
## | ||
#convert an Atom | ||
|
||
at = Atom(6, SA[1.0, 2.0, 3.0]u"Å"; atomic_mass = 1.0u"u") | ||
x = DP.atom(at; properties = (position, atomic_mass, atomic_symbol)) | ||
display(x) | ||
@test x.𝐫 == position(x) == position(at) | ||
@test x.m == atomic_mass(x) == atomic_mass(at) | ||
@test x.Z == atomic_symbol(x) == atomic_symbol(at) | ||
|
||
|
||
## | ||
# convert an entire system | ||
|
||
sys = rattle!(bulk(:Si, cubic=true) * 2, 0.1); | ||
aos = DP.AosSystem(sys); | ||
|
||
aos[1] | ||
aos[1, position] | ||
aos[1, atomic_mass] | ||
atomic_mass(aos, 1) | ||
|
||
get_cell(aos) |