Skip to content

Commit

Permalink
Add custom == methods for Particle and ParticleList.
Browse files Browse the repository at this point in the history
  • Loading branch information
michakraus committed Oct 28, 2022
1 parent 090c8ee commit 1c9dd9f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/particle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ function Particle(DT, len; kwargs...)
Particle(MVector{len}(zeros(DT, len)); kwargs...)
end

Base.:(==)(p1::Particle{T1,ST1}, p2::Particle{T2,ST2}) where {T1,T2,ST1,ST2} = (
T1 == T2 && ST1 == ST2
&& p1.state == p2.state
&& p1.views == p2.views
&& p1.params == p2.params)

@inline function Base.hasproperty(::Particle{T,ST,VT,PT}, s::Symbol) where {T,ST,VT,PT}
hasfield(VT, s) || hasfield(PT, s) || hasfield(Particle, s)
end
Expand Down
9 changes: 9 additions & 0 deletions src/particle_list.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ function ParticleList(x::AbstractVector{DT}, v::AbstractVector{DT}, w::AbstractV
ParticleList(reshape(x, (1,length(x))), reshape(v, (1,length(v))), reshape(w, (1,length(w))); kwargs...)
end

Base.:(==)(pl1::ParticleList{T1,ST1}, pl2::ParticleList{T2,ST2}) where {T1,T2,ST1,ST2} = (
T1 == T2 && ST1 == ST2
&& pl1.list == pl2.list
&& pl1.views == pl2.views
&& pl1.params == pl2.params
&& pl1.particles == pl2.particles
&& pl1.variables == pl2.variables
&& pl1.indices == pl2.indices)

@inline function Base.hasproperty(::ParticleList{T,ST,VT,PT}, s::Symbol) where {T,ST,VT,PT}
hasfield(VT, s) || hasfield(PT, s) || hasfield(Particle, s)
end
Expand Down
2 changes: 1 addition & 1 deletion test/particle_list_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ plh5 = ParticleList(h5file)
@test plh5.v == pl.v
@test plh5.z == pl.z

@test plh5[1] == pl[1] # TODO activate once parameters are stored and read
@test plh5[1] == pl[1]
@test plh5[1,1] == pl[1,1]
@test plh5[1,:] == pl[1,:]
@test plh5[:,1] == pl[:,1]

0 comments on commit 1c9dd9f

Please sign in to comment.