Skip to content

Commit

Permalink
remove AbstractParticle, keep SimpleAtom for now
Browse files Browse the repository at this point in the history
Signed-off-by: Rachel Kurchin <rkurchin@cmu.edu>
  • Loading branch information
rkurchin committed Nov 10, 2021
1 parent a615a79 commit fede982
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 51 deletions.
2 changes: 0 additions & 2 deletions src/AtomsBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@ include("interface.jl")
include("implementation_soa.jl")
include("implementation_aos.jl")

# Write your package code here.

end
7 changes: 4 additions & 3 deletions src/implementation_aos.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ using StaticArrays
export FlexibleSystem

# TODO Switch order of type arguments?
struct FlexibleSystem{D,ET,AT<:AbstractParticle{ET}} <: AbstractSystem{D,ET,AT}
box::SVector{D,<:SVector{D,<:Unitful.Length}}
struct FlexibleSystem{D,ET,L,AT} <: AbstractSystem{D,ET}
box::SVector{D,<:SVector{D,L}}
boundary_conditions::SVector{D,<:BoundaryCondition}
particles::Vector{AT}
FlexibleSystem(box, boundary_conditions, particles) = new{length(boundary_conditions),eltype(elements),eltype(eltype(box)),eltype(particles)}(box, boundary_conditions, particles)
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}
) where {BC<:BoundaryCondition,L<:Unitful.Length,AT}
D = length(box)
if !all(length.(box) .== D)
throw(ArgumentError("box must have D vectors of length D"))
Expand Down
10 changes: 5 additions & 5 deletions src/implementation_soa.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ using StaticArrays

export FastSystem

struct FastSystem{D,ET,AT<:AbstractParticle{ET},L<:Unitful.Length} <:
AbstractSystem{D,ET,AT}
struct FastSystem{D,ET,L<:Unitful.Length} <:
AbstractSystem{D,ET}
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))}(
new{length(boundary_conditions),eltype(elements),eltype(eltype(positions))}(
box,
boundary_conditions,
positions,
Expand Down Expand Up @@ -54,8 +54,8 @@ 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)
Base.length(sys::FastSystem{D,ET}) where {D,ET} = 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} =
Base.getindex(sys::FastSystem{D,ET}, i::Int) where {D,ET} =
SimpleAtom{D}(sys.positions[i], sys.elements[i])
48 changes: 7 additions & 41 deletions src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using PeriodicTable
using StaticArrays
import Base.position

export AbstractParticle, AbstractAtom, AbstractSystem, AbstractAtomicSystem
export AbstractSystem, AbstractAtomicSystem
export ChemicalElement, SimpleAtom
export BoundaryCondition, DirichletZero, Periodic
export atomic_mass,
Expand All @@ -19,36 +19,8 @@ export atomic_mass,
export atomic_property, has_atomic_property, atomic_propertynames
export n_dimensions

#
# A distinguishable particle, can be anything associated with coordinate
# information (position, velocity, etc.)
# most importantly: Can have any identifier type
#
# IdType: Type used to identify the particle
#
abstract type AbstractParticle{ET} end
velocity(::AbstractParticle)::AbstractVector{<:Unitful.Velocity} = missing
position(::AbstractParticle)::AbstractVector{<:Unitful.Length} = error("Implement me")
(element(::AbstractParticle{ET})::ET) where {ET} = error("Implement me")


#
# The atom type itself
# - The atom interface is read-only (to allow as simple as possible implementation)
# Writability may be supported in derived or concrete types.
# - The inferface is only in Cartesian coordinates.
# - Has atom-specific defaults (i.e. assumes every entity represents an atom or ion)
#

const AbstractAtom = AbstractParticle{Element}
element(::AbstractAtom)::Element = error("Implement me")


# Extracting things ... it might make sense to make some of them writable in concrete
# implementations, therefore these interfaces are forwarded from the Element object.
atomic_symbol(atom::AbstractAtom) = element(atom).symbol
atomic_number(atom::AbstractAtom) = element(atom).number
atomic_mass(atom::AbstractAtom) = element(atom).atomic_mass
velocity(p)::AbstractVector{<:Unitful.Velocity} = missing
position(p)::AbstractVector{<:Unitful.Length} = error("Implement me")

#
# Identifier for boundary conditions per dimension
Expand All @@ -63,7 +35,7 @@ struct Periodic <: BoundaryCondition end # Periodic BCs
# Again readonly.
#

abstract type AbstractSystem{D,ET,AT<:AbstractParticle{ET}} end
abstract type AbstractSystem{D,ET} end
(bounding_box(::AbstractSystem{D})::SVector{D,SVector{D,<:Unitful.Length}}) where {D} =
error("Implement me")
(boundary_conditions(::AbstractSystem{D})::SVector{D,BoundaryCondition}) where {D} =
Expand All @@ -86,7 +58,7 @@ Base.lastindex(s::AbstractSystem) = length(s)
Base.iterate(S::AbstractSystem, i::Int=1) = (1 <= i <= length(S)) ? (@inbounds S[i], i+1) : nothing

# iteration interface, needed for default broadcast dispatches below to work
Base.iterate(sys::AbstractSystem{D,ET,AT}, state = firstindex(sys)) where {D,ET,AT} =
Base.iterate(sys::AbstractSystem{D,ET}, state = firstindex(sys)) where {D,ET} =
state > length(sys) ? nothing : (sys[state], state + 1)

# TODO Support similar, push, ...
Expand All @@ -101,15 +73,15 @@ element(sys::AbstractSystem) = element.(sys)
#
# Extra stuff only for Systems composed of atoms
#
const AbstractAtomicSystem{D,AT<:AbstractAtom} = AbstractSystem{D,Element,AT}
const AbstractAtomicSystem{D} = AbstractSystem{D,Element}
atomic_symbol(sys::AbstractAtomicSystem) = atomic_symbol.(sys)
atomic_number(sys::AbstractAtomicSystem) = atomic_number.(sys)
atomic_mass(sys::AbstractAtomicSystem) = atomic_mass.(sys)
atomic_property(sys::AbstractAtomicSystem, property::Symbol)::Vector{Any} =
atomic_property.(sys, property)
atomic_propertiesnames(sys::AbstractAtomicSystem) = unique(sort(atomic_propertynames.(sys)))

struct SimpleAtom{D} <: AbstractAtom
struct SimpleAtom{D}
position::SVector{D,<:Unitful.Length}
element::Element
end
Expand All @@ -122,12 +94,6 @@ function SimpleAtom(position, symbol::Union{Integer,AbstractString,Symbol,Abstra
end

# Just to make testing a little easier for now
function Base.show(io::IO, ::MIME"text/plain", part::AbstractParticle)
print(io, "Particle(", element(part), ") @ ", position(part))
end
function Base.show(io::IO, ::MIME"text/plain", part::AbstractAtom)
print(io, "Atom(", atomic_symbol(part), ") @ ", position(part))
end
function Base.show(io::IO, mime::MIME"text/plain", sys::AbstractSystem)
println(io, "System:")
println(io, " BCs: ", boundary_conditions(sys))
Expand Down

0 comments on commit fede982

Please sign in to comment.