在 Julia 中模拟盒子内粒子的碰撞

时间:2021-07-15 09:04:36

标签: julia collision-detection linear-algebra particles

我想编写一个函数(我们称之为 [filter "hack-debug-lines-in-python"] clean = ... smudge = ... ),更新盒子内粒子的位置和速度。

![enter image description here

我已经有一个函数来描述两个粒子之间的碰撞。

.git/config
update!

我知道函数 mutable struct Particle pos :: Vector{Float64} vel :: Vector{Float64} end p1 = Particle( rand(2) , rand(2) ) p2 = Particle( rand(2) , rand(2) ) 必须:

  1. 找出粒子是否不太近(它们不能比 2*Radius 更近,如上图所示)。
  2. 仅考虑相关粒子。
    对于粒子 p1,我们可以使用 function collision!(p1::Particle, p2::Particle) # Find collision vector n = p1.pos - p2.pos # Normalize it, since you want an orthonormal basis n ./= sqrt(n[1]^2 + n[2]^2) # Construct M M = [n[1] n[2]; -n[2] n[1]] # Find transformed velocity vectors v1ₙ = M*p1.vel v2ₙ = M*p2.vel # Swap first component v1ₙ[1], v2ₙ[1] = v2ₙ[1], v1ₙ[1] # Calculate and store new velocity vectors p1.vel .= M'*v1ₙ p2.vel .= M'*v2ₙ return nothing end 处理与其他粒子的所有可能碰撞。
  3. 看看粒子是否在盒子里面,如果不是,那么它会从盒子的壁上“弹开”。
    (我知道这可以使用 update! 来完成)
collsion!
  1. 最后应该有另一个函数,它定义了粒子数、框大小、时间间隔 dt 和模拟运行的时间量。
xlims, ylims

我认为我的理论是正确的,但我不确定如何实施它。任何帮助将不胜感激。

0 个答案:

没有答案