-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from JuliaMolSim/soa
SoA example
- Loading branch information
Showing
5 changed files
with
166 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using AtomsBase | ||
using StaticArrays | ||
using Unitful | ||
|
||
box = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]u"m" | ||
bcs = [Periodic(), Periodic(), Periodic()] | ||
positions = [0.25 0.25 0.25; 0.75 0.75 0.75]u"m" | ||
elements = [ChemicalElement(:C), ChemicalElement(:C)] | ||
|
||
atom1 = SimpleAtom(SVector{3}(positions[1,:]),elements[1]) | ||
atom2 = SimpleAtom(SVector{3}(positions[2,:]),elements[2]) | ||
|
||
aos = FlexibleSystem(box, bcs, [atom1, atom2]) | ||
soa = FastSystem(box, bcs, positions, elements) | ||
|
||
# And now we can ask questions like... | ||
soa .== aos |
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,35 @@ | ||
# Example implementation using as few function definitions as possible | ||
# | ||
using StaticArrays | ||
|
||
export FlexibleSystem | ||
|
||
# TODO Switch order of type arguments? | ||
struct FlexibleSystem{D,ET<:AbstractElement,AT<:AbstractParticle{ET}} <: AbstractSystem{D,ET,AT} | ||
box::SVector{D,<:SVector{D,<:Unitful.Length}} | ||
boundary_conditions::SVector{D,<:BoundaryCondition} | ||
particles::Vector{AT} | ||
end | ||
# convenience constructor where we don't have to preconstruct all the static stuff... | ||
function FlexibleSystem( | ||
box::Vector{Vector{L}}, | ||
boundary_conditions::Vector{BC}, | ||
particles::Vector{AT}, | ||
) where {BC<:BoundaryCondition,L<:Unitful.Length,AT<:AbstractParticle} | ||
D = length(box) | ||
if !all(length.(box) .== D) | ||
throw(ArgumentError("box must have D vectors of length D")) | ||
end | ||
FlexibleSystem(SVector{D,SVector{D,L}}(box), SVector{D,BoundaryCondition}(boundary_conditions), particles) | ||
end | ||
|
||
bounding_box(sys::FlexibleSystem) = sys.box | ||
boundary_conditions(sys::FlexibleSystem) = sys.boundary_conditions | ||
|
||
function Base.show(io::IO, ::MIME"text/plain", sys::FlexibleSystem) | ||
print(io, "FlexibleSystem with ", length(sys), " particles") | ||
end | ||
|
||
Base.size(sys::FlexibleSystem) = size(sys.particles) | ||
Base.length(sys::FlexibleSystem) = length(sys.particles) | ||
Base.getindex(sys::FlexibleSystem, i::Int) = getindex(sys.particles, i) |
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,61 @@ | ||
# Example implementation using as few function definitions as possible | ||
# | ||
using StaticArrays | ||
|
||
export FastSystem | ||
|
||
struct FastSystem{D,ET<:AbstractElement,AT<:AbstractParticle{ET},L<:Unitful.Length} <: | ||
AbstractSystem{D,ET,AT} | ||
box::SVector{D,SVector{D,L}} | ||
boundary_conditions::SVector{D,BoundaryCondition} | ||
positions::Vector{SVector{D,L}} | ||
elements::Vector{ET} | ||
# janky inner constructor that we need for some reason | ||
FastSystem(box, boundary_conditions, positions, elements) = | ||
new{length(boundary_conditions),eltype(elements),SimpleAtom,eltype(eltype(positions))}( | ||
box, | ||
boundary_conditions, | ||
positions, | ||
elements, | ||
) | ||
end | ||
|
||
# convenience constructor where we don't have to preconstruct all the static stuff... | ||
function FastSystem( | ||
box::AbstractVector{Vector{L}}, | ||
boundary_conditions::AbstractVector{BC}, | ||
positions::AbstractMatrix{M}, | ||
elements::AbstractVector{ET}, | ||
) where {L<:Unitful.Length,BC<:BoundaryCondition,M<:Unitful.Length,ET<:AbstractElement} | ||
N = length(elements) | ||
D = length(box) | ||
if !all(length.(box) .== D) | ||
throw(ArgumentError("box must have D vectors of length D")) | ||
end | ||
if !(size(positions, 1) == N) | ||
throw(ArgumentError("box must have D vectors of length D")) | ||
end | ||
if !(size(positions, 2) == D) | ||
throw(ArgumentError("box must have D vectors of length D")) | ||
end | ||
FastSystem( | ||
SVector{D,SVector{D,L}}(box), | ||
SVector{D,BoundaryCondition}(boundary_conditions), | ||
Vector{SVector{D,eltype(positions)}}([positions[i, :] for i = 1:N]), | ||
elements, | ||
) | ||
end | ||
|
||
function Base.show(io::IO, ::MIME"text/plain", sys::FastSystem) | ||
print(io, "FastSystem with ", length(sys), " particles") | ||
end | ||
|
||
bounding_box(sys::FastSystem) = sys.box | ||
boundary_conditions(sys::FastSystem) = sys.boundary_conditions | ||
|
||
# Base.size(sys::FastSystem) = size(sys.particles) | ||
Base.length(sys::FastSystem{D,ET,AT}) where {D,ET,AT} = length(sys.elements) | ||
|
||
# first piece of trickiness: can't do a totally abstract dispatch here because we need to know the signature of the constructor for AT | ||
Base.getindex(sys::FastSystem{D,ET,SimpleAtom}, i::Int) where {D,ET} = | ||
SimpleAtom{D}(sys.positions[i], sys.elements[i]) |
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