using StaticArrays: SVector using LoopVectorization: @turbo mutable struct Particle id::Int64 c::Vector{Float64} # Center tmp_c::Vector{Float64} # Temporary center φ::Float64 # Angle function Particle(; id::Int64, c::Vector{Float64}, φ::Float64) return new(id, c, zeros(2), φ) end end function restrict_coordinate(value::Float64; l::Float64) if value < -l value += 2 * l elseif value >= l value -= 2 * l end return value end function restrict_coordinates!(p::Particle; l::Float64) @simd for i in 1:2 p.tmp_c[i] = restrict_coordinate(p.tmp_c[i]; l=l) end return nothing end function minimum_image_coordinate(value::Float64; l::Float64) if value <= -l value += 2 * l elseif value > l value -= 2 * l end return value end function minimum_image(v::SVector{2,Float64}; l::Float64) return SVector(minimum_image_coordinate(v[1]; l=l), minimum_image_coordinate(v[2]; l=l)) end function are_overlapping(p1::Particle, p2::Particle, overlapping_r²::Float64, l::Float64) r⃗₁₂ = SVector(p2.c[1] - p1.c[1], p2.c[2] - p1.c[2]) # 1 -> 2 r⃗₁₂ = minimum_image(r⃗₁₂; l=l) distance² = r⃗₁₂[1]^2 + r⃗₁₂[2]^2 overlapping = distance² < overlapping_r² return (; overlapping, r⃗₁₂, distance²) end function update!(p::Particle) @turbo for i in 1:2 p.c[i] = p.tmp_c[i] end return nothing end