Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
summer_2019:ideal_gas_law [2019/08/02 00:41] wellerd |
summer_2019:ideal_gas_law [2019/08/06 15:27] (current) wellerd |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | Copy and paste the following code into Glowscript to model the ideal gas law. | + | ====== Ideal Gas Law Activity ====== |
+ | **Follow this link for the activity and the instructions: [[https://trinket.io/glowscript/575630aab8?showInstructions=true|link]]** | ||
- | <code> | + | **Or, read the instructions after the image below, and copy the code into your own GlowScript file.** |
- | GlowScript 2.8 VPython | + | |
- | ## Constants | + | |
- | R=8.314 # Gas constant | + | |
- | N_Avo=6.02E23 # Avogodro's constant | + | |
- | L = 0.1 # Our container is a cube with L=0.1m on each side | + | |
- | Vol=L*L*L # Volume of our cube is 1 Liter | + | |
- | Atomic_radius = 0.001 # wildly exaggerated size of a gas atom | + | |
- | container = box(pos=vec(0,0,0),size=vec(L,L,L), color=color.white, opacity=0.1) # Create a container for our gas atoms | + | You should see something that looks like this: |
- | N_atoms = 300 # Number of gas atoms in your simulation | + | {{:summer_2019:idealgasparticle.png?800|}} |
- | n=N_atoms/N_Avo # number of moles of gas atoms | + | |
- | Molar_mass = 4E-3 # molar mass of helium in kg/mol | + | If you click on the "Instructions" tab in the upper right, a set of instructions for the activity should pop up. Click between "Instructions" and "Result" to alternately view the instructions and the animation. If you prefer, the same instructions are also listed below. |
- | T = 300 # temperature in Kelvin | + | * Try runnning your code. You'll notice that our gas particle is currently escaping the box. |
+ | |||
+ | * Add an 'if' statement to check for when the gas particle collides with a wall, and make the particle move in th opposite direction after contacting the wall. | ||
- | #v_avg = sqrt(8*R*T/(pi*Molar_mass)) # average velocity of gas atoms | + | * Enter the equation for v_rms of a gas particle and correct that value in your particle's velocity. |
- | v_rms = sqrt(3*R*T/(Molar_mass)) # root mean squared velocity of gas atoms | + | |
- | Velocity=v_rms #Which velocity would you like to assign your gas atoms: v_avg or v_rms? | + | |
- | Theoretical_Pressure=n*R*T/Vol # Calculate the theoretical pressure based on the ideal gas law | + | * The pressure of an ideal gas comes from collisions between particles and the walls of the container. The pressure is equal to the force (mass times delta_velocity) divided by the area. Add a line to the code that calculates the pressure from our gas atom colliding with the wall. |
+ | * Hint: Everytime that a particle-wall collision occurs, you should increase your pcount, and then average the total pressure by dividing by pcount. | ||
- | ## The lines below create a graph for the heigh vs time. Try creating a graph for the velocity and acceleration | + | * Try to create a pressure vs. time graph that adds another data point for every particle-wall collision. In this simplified model, the pressure versus time graph should be horizontal. |
- | Grph1 = graph(title='Pressure vs Time', xtitle='Time (s)', ytitle='Pressure (atm)', fast=False, ymin=0, ymax=5*Theoretical_Pressure) #initialize our graphs. Useful boundaries: ymin=0, ymax=5*Theoretical_Pressure | + | |
- | ExperimentalPressureGraph = gcurve(color=color.red, label='Experimental_Pressure') #Make a graph for measured pressure | + | * For an extra challenge, try making the particle collisions work in three dimensions. This will build into a more complicated model to be used later. |
- | TheoreticalPressureGraph = gcurve(color=color.blue, label='Theoretical_Pressure') #Make a graph for theoretical pressure | + | |
- | + | ||
- | t=0 # initialize the time variable | + | |
- | dt = 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 | + | For more information on glowscript tools, check out: [[https://www.glowscript.org/docs/GlowScriptDocs/index.html]] |
- | ListOfParticles = [] # Empty list of particles | + | |
- | for i in range(0,N_atoms): #Loop over all of the atoms | + | |
- | newparticle = sphere(pos=vector(L*random()-L/2,L*random()-L/2,L*random()-L/2),radius=Atomic_radius, 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_Avo #Assign particles the atomic mass (in kg/atom) of your gas | + | |
- | ListOfParticles.append(newparticle) #Append the particle to our list of particles | + | |
- | ## The following lines simulate random motion and particle-wall collisions | + | <code> |
- | while True: #Run infinitely | + | GlowScript 2.7 VPython |
- | rate(9999) #Determines how fast the code runs | + | ## Constants |
- | for particle in ListOfParticles: #Loop over all partciles | + | L=0.1 #Give our container a length of 0.1m on each side |
- | particle.pos = particle.pos + particle.velocity*dt #Update position of every particle based on its velocity | + | N_Avogodro=6.02E23 # Avogodro's constant |
- | + | k_B=1.38E-23 # Boltzmann constant | |
- | #Check for particle-wall collisions in x,y,z. If there is a collision, reverse the velocity direction and add some pressure to the total. | + | |
- | if abs(particle.pos.x) >= L/2: | + | |
- | particle.velocity.x = - particle.velocity.x | + | |
- | pressure+=particle.mass*abs(particle.velocity.x)/(L*L*dt) | + | |
- | pcount+=1 | + | |
- | if abs(particle.pos.y) >= L/2: | + | |
- | particle.velocity.y = - particle.velocity.y | + | |
- | pressure+=particle.mass*abs(particle.velocity.y)/(L*L*dt) | + | |
- | pcount+=1 | + | |
- | if abs(particle.pos.z) >= L/2: | + | |
- | particle.velocity.z = - particle.velocity.z | + | |
- | pressure+=particle.mass*abs(particle.velocity.z)/(L*L*dt) | + | |
- | pcount+=1 | + | |
- | + | ||
- | #The following lines calculate the experimental pressure measured due to particle-wall collisions | + | |
- | if pcount>=10: #After at least 10 particle-wall collisions occur | + | |
- | Experimental_Pressure=pressure/pcount # Average the total pressure over the number of particle-wall collisions | + | |
- | Experimental_Pressure=Experimental_Pressure/101.325 # Convert from kPa 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 | + | ## Gas information |
+ | mass = 4E-3/N_Avogodro # helium mass in kg/atom | ||
+ | Ratom=0.01 # exaggerated size of helium atom | ||
+ | T=300 # Temperature equals 300 K | ||
- | t=t+dt #move onto the next time-step | + | v_rms=0 # Calculate the root-mean square speed |
+ | |||
+ | ## Setup a container with a gas particle inside | ||
+ | container = box(pos=vec(0,0,0), size=vec(L+2*Ratom,L+2*Ratom,L+2*Ratom), color=color.white, opacity=0.1) | ||
+ | particle = sphere(pos=vec(0,0,0), radius = Ratom, color = color.red) | ||
+ | particle.velocity=vec(500,0,0) | ||
+ | |||
+ | ## Create a graph to track pressure | ||
+ | Grph1 = graph(title='Pressure vs Time', xtitle='Time (s)', ytitle='Pressure (Pa/atom)', fast=False, ymin=0, ymax=1E-20) #initialize our graphs. Useful boundaries: ymin=0, ymax=5*Theoretical_Pressure | ||
+ | ExperimentalPressureGraph = gcurve(color=color.red, label='Experimental_Pressure') #Make a graph for measured pressure | ||
+ | |||
+ | ## Set up the time variables for the while loop | ||
+ | dt = 1E-7 # Time-step | ||
+ | t = 0 # Initialize time variable | ||
+ | pressure=0 # initialize the pressure variable | ||
+ | pcount = 0 # initialize a pressure counter | ||
+ | |||
+ | ## While loop to iterate over time | ||
+ | while True: | ||
+ | rate(1000) # Determines how fast the simulation runs | ||
+ | particle.pos = particle.pos + particle.velocity*dt # Update the particle's position | ||
+ | |||
+ | ## Add if statement for particle-wall collision here | ||
+ | |||
+ | |||
+ | ## Add a graph for experimental pressure here | ||
+ | |||
+ | |||
+ | t = t + dt | ||
</code> | </code> |