This is an old revision of the document!


GlowScript 2.8 VPython
## Constants
R=8.314 # Gas constant
N_A=6.02E23 # Avogodro's constant
L = 0.1 # Our container is a cube with L=0.1m on each side
Ratom = 0.001 # wildly exaggerated size of a gas atom

## Properties of the gas atoms we are working with
N_atoms = 500 # change this to have more or fewer atoms
n=N_atoms/N_A # number of moles of gas atoms

Molar_mass = 4E-3 # molar mass of helium in kg/mol

T = 300 # temperature in Kelvin

## Create a container for our gas atoms
container = box(pos=vec(0,0,0),size=vec(L,L,L), color=color.white, opacity=0.1)

v_avg = sqrt(8*R*T/(pi*Molar_mass)) # average velocity of gas atoms
#v_rms = sqrt(3*R*T/(Molar_mass)) # root mean squared velocity of gas atoms
Velocity=v_avg

Theoretical_Pressure=n*R*T/(L*L*L*1000) # Calculate the theoretical pressure based on the ideal gas law

## The lines below create a graph for the heigh vs time. Try creating a graph for the velocity and acceleration
Grph1 = graph(title='Pressure vs Time', xtitle='Time (s)', ytitle='Pressure (atm)', fast=False,ymin=0, ymax=5*Theoretical_Pressure) #ymin=0, ymax=2*Theoretical_Pressure
ExperimentalPressureGraph = gcurve(color=color.red, label='Experimental_Pressure')
TheoreticalPressureGraph = gcurve(color=color.blue, label='Theoretical_Pressure')
    
t=0 # initialize the time variable
dt = 2.5E-9 #time-step interval
pressure=0 # initialize the pressure tracker
pcount=0 # This counter will track when a particle-wall collision adds to the pressure

## The following lines create all the particles
ListOfParticles = [] # Empty list of particles
for i in range(0,N_atoms):
    newparticle = sphere(pos=vector(L*random()-L/2,L*random()-L/2,L*random()-L/2),radius=Ratom, color=color.green,opacity=0.7, visible = True) #Create a spherical particle at a random postion
    newparticle.velocity = vector(Velocity*sin(pi*random())*cos(2*pi*random()),Velocity*sin(pi*random())*sin(2*pi*random()),Velocity*cos(pi*random())) #Randomize velocity
    newparticle.mass = Molar_mass/N_A #Give the particles the atomic mass of your gas
    ListOfParticles.append(newparticle) #Append the particle to our list of particles

## The following lines simulate random motion and particle-wall collisions
while True:
    rate(9999)
    for particle in ListOfParticles:
        particle.pos = particle.pos + particle.velocity*dt #Update position of every particle based on its velocity
        
        #Check for particle-wall collisions in x,y,z. If there are collisions, add some pressure to the total.
        if abs(particle.pos.x) >= L/2:
            particle.velocity.x = - particle.velocity.x 
            pcount+=1 
            pressure+=particle.mass*abs(particle.velocity.x)/(L*L*dt)
        if abs(particle.pos.y) >= L/2:
            particle.velocity.y = - particle.velocity.y  
            pcount+=1 
            pressure+=particle.mass*abs(particle.velocity.y)/(L*L*dt)
        if abs(particle.pos.z) >= L/2:
            particle.velocity.z = - particle.velocity.z 
            pcount+=1 
            pressure+=particle.mass*abs(particle.velocity.z)/(L*L*dt)
    
    #The following lines calculate the experimental pressure measured due to particle-wall collisions
    if pcount>=1:
        Experimental_Pressure=pressure/pcount # Average the total pressure over the number of particle-wall collisions
        Experimental_Pressure=Experimental_Pressure*101325 # Convert from Pa to atm
        ExperimentalPressureGraph.plot(t,Experimental_Pressure) #Graph the experimental pressure
        pressure=0 # Reset the pressure tracker
        pcount=0 # Reset the pressure counter

    TheoreticalPressureGraph.plot(t,Theoretical_Pressure) #Graph theoretical pressure

    t=t+dt
  • summer_2019/ideal_gas_law.1564699582.txt.gz
  • Last modified: 2019/08/01 22:46
  • by wellerd