【问题标题】:Lennard Jones interaction between particles. Particles moving to one pointLennard Jones 粒子之间的相互作用。粒子移动到一点
【发布时间】:2015-04-22 13:28:16
【问题描述】:
import numpy as np
import random
import pygame
background_colour = (255,255,255)
width, height = 300, 325
eps = 1
sigma = 1
dt = 0.05

class Particle():
    def __init__(self):
        self.x = random.uniform(0,400)
        self.y = random.uniform(0,500)
        self.vx = random.uniform(-.1,.1)
        self.vy = random.uniform(-.1,.1)
        self.fx = 0
        self.fy = 0
        self.m = 1
        self.size = 10
        self.colour = (0, 0, 255)
        self.thickness = 0

    def bounce(self):
        if self.x > width - self.size:
            self.x = 2*(width - self.size) - self.x

        elif self.x < self.size:
            self.x = 2*self.size - self.x

        if self.y > height - self.size:
            self.y = 2*(height - self.size) - self.y

        elif self.y < self.size:
            self.y = 2*self.size - self.y
    def getForce(self, p2):
        dx = self.x - p2.x
        dy = self.y - p2.y
        self.fx = 500*(-8*eps*((3*sigma**6*dx/(dx**2+dy**2)**4 - 6*sigma**12*dx/(dx**2+dy**2)**7)))
        self.fy = 500*(-8*eps*((3*sigma**6*dy/(dx**2+dy**2)**4 - 6*sigma**12*dy/(dx**2+dy**2)**7)))
        return self.fx, self.fy

    def verletUpdate(self,dt):
        self.x = self.x + dt*self.vx+0.5*dt**2*self.fx/self.m
        self.y = self.y + dt*self.vy+0.5*dt**2*self.fy/self.m
    def display(self):
       pygame.draw.circle(screen, self.colour, (int(self.x), int(self.y)), self.size, self.thickness)
screen = pygame.display.set_mode((width, height))
screen.fill(background_colour) 

partList = []
for k in range(10):
    partList.append(Particle())

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    screen.fill(background_colour)

    for k, particle in enumerate(partList):
        for p2 in partList[k+1:]:
            particle.getForce(p2)
        particle.verletUpdate(dt)
        particle.bounce()
        particle.display()

    pygame.display.flip()
pygame.quit()

我的代码正确吗?我试图用 Lennard Jones 力模拟二维运动中的粒子。我认为计算力没问题,但为什么我的粒子会移动到一点?有时我也会收到错误 OverflowError: Python int too large to convert to C long 任何建议都会很有用。

【问题讨论】:

  • 不,您的代码不正确。你的力量不是由 Lennart-Jones 潜力产生的力量。你没有积累相互作用力。您的 verlet 函数没有实现 Verlet,因为它不会更新速度。您不能混合交互计算和位置/速度更新,您需要在粒子上进行 3 或 4 个单独的循环。
  • @LutzL 那么我该如何改进呢

标签: python pygame simulation physics scientific-computing


【解决方案1】:

我无法评论模拟的物理特性,但就显示而言,以下是我的观察:

您的粒子移动到一个点,因为verletUpdate 中代码中 x 和 y 参数的更新条件正在缓慢移动到显示区域之外的值。还有超出导致错误的 int() 函数范围的值。您可以通过以下语句看到这一点:

def verletUpdate(self,dt):
    self.x = self.x + dt*self.vx+0.5*dt**2*self.fx/self.m
    self.y = self.y + dt*self.vy+0.5*dt**2*self.fy/self.m
    print self.x
    print self.y

样本输出:

290.034892392
9.98686293664
290.028208837
9.99352484332
-2.55451579742e+19
1.12437640586e+19

它们也会饱和,随着迭代,更新变得越来越小:

def display(self):
          print ' %s + %s '%(self.x,self.y)
          pygame.draw.circle(screen, self.colour, (int(self.x), int(self.y)), self.size, self.thickness)

输出:

 10.0009120033 + 10.0042647307 
 10.0009163718 + 10.0000322065 
 10.0009120033 + 10.0042647307 
 10.0009163718 + 10.0000322065 
 ...
 10.0009163718 + 10.0000322065 
 10.0009120033 + 10.0042647307 
 10.0009163718 + 10.0000322065 

这也是您的反弹功能和限制检查不起作用的原因。有时经过多次迭代,你的 self.x 和 self.y 远远超出了int() 的限制。

代码看起来不错,但是您可以通过在绘制线上方添加一些检查来消除溢出错误。例如,我再次随机初始化它们以模拟粒子离开屏幕并跟踪新粒子。随意改变它。

   def display(self):
          if(self.x<0 or self.x>height):
              self.__init__()
              print "reset"
          if(self.y<0 or self.y>width):
              self.__init__()
              print "reset"
          print ' %s + %s '%(self.x,self.y)
          pygame.draw.circle(screen, self.colour, (int(self.x), int(self.y)), self.size, self.thickness)

还有一次,您将数组寻址为 [k+1:],并且寻址零元素会导致除以零错误。你可能想看看那个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 2018-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多