大都会蒙特卡罗理想气体模拟体积到z

2024-06-06 16:07:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用大都会蒙特卡罗方法,根据克拉珀龙方程,对理想气体进行简单的模拟算法。这个这是一个非常简单的例子,我认为分子在二维空间中,相互之间没有相互作用,能量等于E=pV,V是包含所有分子的圆的面积。你知道吗

我的问题是,经过很少的蒙特卡罗步骤,我的气体体积总是几乎为零,不管我加了多少分子或压力,我不知道我的代码中是否有错误,或者是因为我没有任何分子相互作用。 所有的帮助将是非常感谢,这是我的代码

我的结果显示在下面的曲线图中,x轴是蒙特卡罗步数,y轴是体积,我希望结果是,经过更多的步数后,体积的一些非零常数值

import numpy as np
import random
import matplotlib.pyplot as plt


def centroid(arr):
    length = arr.shape[0]
    sum_x = sum([i.x for i in arr])
    sum_y = sum([i.y for i in arr])
    return sum_x/length, sum_y/length


class Molecule:
    def __init__(self, xpos, ypos):
        self.pos = (xpos, ypos)
        self.x = xpos
        self.y = ypos


class IdealGas:
    # CONSTANTS
    def __init__(self, n,full_radius, pressure, T, number_of_runs):
        gas = []
        for i in range(0, n):
            gas.append(Molecule(random.random() * full_radius,
                                random.random() * full_radius))
        self.gas = np.array(gas)
        self.center = centroid(self.gas)
        self.small_radius = full_radius/4.
        self.pressure = pressure
        self.kbT = 9.36E-18 * T

        self.n = n
        self.number_of_runs = number_of_runs

    def update_pos(self):
        random_molecule = np.random.choice(self.gas)
        old_state_x = random_molecule.x
        old_state_y = random_molecule.y
        old_radius = np.linalg.norm(np.array([old_state_x,old_state_y])-np.array([self.center[0],self.center[1]]))
        energy_old = np.pi * self.pressure * old_radius**2
        random_molecule.x = old_state_x + (random.random() * self.small_radius) * np.random.choice([-1, 1])
        random_molecule.y = old_state_y + (random.random() * self.small_radius) * np.random.choice([-1, 1])
        new_radius =  np.linalg.norm(np.array([random_molecule.x,random_molecule.y])-np.array([self.center[0],self.center[1]]))
        energy_new = np.pi * self.pressure * new_radius**2
        if energy_new - energy_old <= 0.0:
            return random_molecule
        elif np.exp((-1.0 * (energy_new - energy_old)) / self.kbT) > random.random():
            return random_molecule
        else:
            random_molecule.x = old_state_x
            random_molecule.y = old_state_y
            return random_molecule

    def monte_carlo_step(self):
        gas = []
        for molecule in range(0, self.n):
            gas.append(self.update_pos())
        self.gas = np.array(gas)
        #self.center = centroid(self.gas)
        return self.gas

    def simulation(self):
        volume = []
        for run in range(self.number_of_runs):
            step_gas = self.monte_carlo_step()
            step_centroid = centroid(step_gas)
            step_radius = max([np.linalg.norm(np.array([gas.x,gas.y])-np.array([step_centroid[0],step_centroid[1]]))
                               for gas in step_gas])
            step_volume = np.pi * step_radius**2
            volume.append(step_volume)
        return volume


Gas = IdealGas(500, 50, 1000, 300, 100)
vol = Gas.simulation()
plt.plot(vol)
plt.show()



Tags: inselffordefstepnprandomarray
1条回答
网友
1楼 · 发布于 2024-06-06 16:07:32

只有当新半径小于旧半径时,才允许分子移动:

if energy_new - energy_old <= 0.0:

相当于:

np.pi * self.pressure * new_radius**2 <= np.pi * self.pressure * old_radius**2

即:

abs(new_radius) <= abs(old_radius)

所以所有的分子都流向中心

对我来说,你的假设太强了:你确定了温度、压力和分子数。根据理想气体方程,体积v=nRT/p也是常数。如果体积可以改变,那么压力或温度就必须改变。在你的模拟中,允许压力变化意味着压力和体积的乘积是恒定的,所以能量是恒定的,所以分子可以在很大的体积内自由运动。你知道吗

顺便说一下,我认为分子应该初始化为:

(random.random() - 0.5) * full_radius

这样飞机就占据了零点附近的所有平面。你知道吗

相关问题 更多 >