-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add bonds to AbstractSystem #90
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# | ||
# Implementation of AtomsBase interface in a struct-of-arrays style with bonds. | ||
# | ||
export BondedSystem | ||
|
||
struct BondedSystem{D, L <: Unitful.Length, M <: Unitful.Mass} <: AbstractSystem{D} | ||
bounding_box::SVector{D, SVector{D, L}} | ||
boundary_conditions::SVector{D, BoundaryCondition} | ||
position::Vector{SVector{D, L}} | ||
bonds::Vector{Tuple{Integer, Integer, BondOrder}} | ||
atomic_symbol::Vector{Symbol} | ||
atomic_number::Vector{Int} | ||
atomic_mass::Vector{M} | ||
end | ||
|
||
# Constructor to fetch the types | ||
function BondedSystem(box, boundary_conditions, positions, bonds, atomic_symbols, atomic_numbers, atomic_masses) | ||
BondedSystem{length(box),eltype(eltype(positions)),eltype(atomic_masses)}( | ||
box, boundary_conditions, positions, bonds, atomic_symbols, atomic_numbers, atomic_masses | ||
) | ||
end | ||
|
||
# Constructor to take data from another system | ||
function BondedSystem(system::AbstractSystem) | ||
BondedSystem(bounding_box(system), boundary_conditions(system), position(system), bonds(system), | ||
atomic_symbol(system), atomic_number(system), atomic_mass(system)) | ||
end | ||
|
||
# Convenience constructor where we don't have to preconstruct all the static stuff... | ||
function BondedSystem(particles, box, boundary_conditions, bonds) | ||
D = length(box) | ||
if !all(length.(box) .== D) | ||
throw(ArgumentError("Box must have D vectors of length D=$D.")) | ||
end | ||
if length(boundary_conditions) != D | ||
throw(ArgumentError("Boundary conditions should be of length D=$D.")) | ||
end | ||
if !all(n_dimensions.(particles) .== D) | ||
throw(ArgumentError("Particles must have positions of length D=$D.")) | ||
end | ||
BondedSystem(box, boundary_conditions, position.(particles), bonds, atomic_symbol.(particles), | ||
atomic_number.(particles), atomic_mass.(particles)) | ||
end | ||
|
||
bounding_box(sys::BondedSystem) = sys.bounding_box | ||
boundary_conditions(sys::BondedSystem) = sys.boundary_conditions | ||
bonds(sys::BondedSystem) = sys.bonds | ||
|
||
Base.length(sys::BondedSystem) = length(sys.position) | ||
Base.size(sys::BondedSystem) = size(sys.position) | ||
|
||
species_type(::FS) where {FS <: BondedSystem} = AtomView{FS} | ||
Base.getindex(sys::BondedSystem, i::Integer) = AtomView(sys, i) | ||
|
||
position(s::BondedSystem) = s.position | ||
atomic_symbol(s::BondedSystem) = s.atomic_symbol | ||
atomic_number(s::BondedSystem) = s.atomic_number | ||
atomic_mass(s::BondedSystem) = s.atomic_mass | ||
velocity(::BondedSystem) = missing | ||
|
||
# System property access | ||
function Base.getindex(system::BondedSystem, x::Symbol) | ||
if x === :bounding_box | ||
bounding_box(system) | ||
elseif x === :boundary_conditions | ||
boundary_conditions(system) | ||
elseif x === :bonds | ||
bonds(system) | ||
else | ||
throw(KeyError(x)) | ||
end | ||
end | ||
Base.haskey(::BondedSystem, x::Symbol) = x in (:bounding_box, :boundary_conditions, :bonds) | ||
Base.keys(::BondedSystem) = (:bounding_box, :boundary_conditions, :bonds) | ||
|
||
# Atom and atom property access | ||
atomkeys(::BondedSystem) = (:position, :atomic_symbol, :atomic_number, :atomic_mass) | ||
hasatomkey(system::BondedSystem, x::Symbol) = x in atomkeys(system) | ||
function Base.getindex(system::BondedSystem, i::Union{Integer,AbstractVector}, x::Symbol) | ||
getfield(system, x)[i] | ||
end | ||
Base.getindex(system::BondedSystem, ::Colon, x::Symbol) = getfield(system, x) | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,10 @@ import PeriodicTable | |
|
||
export AbstractSystem | ||
export BoundaryCondition, DirichletZero, Periodic, infinite_box, isinfinite | ||
export bounding_box, boundary_conditions, periodicity, n_dimensions, species_type | ||
|
||
export bounding_box, boundary_conditions, periodicity, n_dimensions, species_type, bonds | ||
export position, velocity, element, element_symbol, atomic_mass, atomic_number, atomic_symbol | ||
|
||
export atomkeys, hasatomkey | ||
|
||
# | ||
|
@@ -51,6 +53,23 @@ Return the type used to represent a species or atom. | |
""" | ||
function species_type end | ||
|
||
""" | ||
The possible bond orders that can be used in the `bonds` function | ||
""" | ||
@enum BondOrder begin | ||
single | ||
double | ||
triple | ||
end | ||
|
||
""" | ||
bonds(::AbstractSystem) | ||
|
||
Returns a Vector{Tuple{Integer, Integer, BondOrder}} where each entry stores the unique indices | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Strictly, |
||
i and j of the atoms participating in the bond and the order of the bond (e.g. [(13,21,5)]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think the example at the end of this line is actually in line with what you want, since the third element should be a |
||
""" | ||
function bonds end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a default implementation that just returns an empty tuple? e.g. instead of this line, something like: bonds(sys::AbstractSystem) = [()] (I'm not sure if there's a type stability issue here with that return type not having the type parameters of the Tuple, though) |
||
|
||
"""Return vector indicating whether the system is periodic along a dimension.""" | ||
periodicity(sys::AbstractSystem) = [isa(bc, Periodic) for bc in boundary_conditions(sys)] | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should probably do this as
because it seems that if you don't do that, it actually assigns them numbers in a zero-indexed fashion (super weird), so the following can happen, which would be very confusing:
Separately, there might be a longer-term discussion about whether we should break out bonding types to be part of the interface since I could imagine people wanting more flexibility here, but my opinion is we start with this and file an issue for future conversations about this.