Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

04 - Functions: Gases

Functions are used all the time and are a way to organize code. They are an efficient way to use the computer to repeat calculations with slight variations. Multiple examples are given here of functions you’ll use in your educational and professional career.

Equations of State: Ideal Gas

Equations of state relate the pressure, temperature, and molar volume of a fluid mathematically. These are used in almost every discipline of engineering problems chemical engineers face. The ideal gas law is the simplest equation of state:

P=nRTV=RTV~P = \frac{nRT}{V} = \frac{RT}{\tilde{V}}

where PP is pressure (absolute), RR is the gas constant, TT is temperature, and V~\tilde{V} is the molar volume (volume of gas divided by the moles of gas). The molar volume is the volume of one mole of a substance. The ideal gas law is a good approximation for many gases at low pressures and high temperatures.

What about an inline way to create the same function? Yes, this can be done with a lambda function. Here is an example of a lambda function that calculates the pressure of an ideal gas.

85433.5172413793

Calculate the Pressure using Our Function

Now we can use this function to calculate the pressure of 1 mole of an ideal gas at 300 K and 1 L. We’ll use the print function to print the result to the screen.

The absolute pressure in a 1 Liter vessel with 1 mole of gas inside is 2494200 Pa

Course Outcome:

  • Students will be able to convert problem solving strategies to procedural algorithms, and

  • make order of magnitude estimates, assess reasonableness of solutions, and select appropriate levels of solution sophistication

Classroom demo and Group exercise: Estimate the number of moles inside a balloon of the size shown in class. The pressure inside a balloon is approximately 0.75 psi (your lungs can generate 1-2 psig). Make a function and then use it to calculate the number of moles of air inside the balloon.

Plot of the Pressure vs Molar Volume for an Ideal Gas

We’ll use our function to create a plot of the pressure vs molar volume for an ideal gas at different temperatures.

<Figure size 640x480 with 1 Axes>

This plot shows that as the volume decreases for a given amount of gas, the pressure increases. The pressure also increases with temperature. We’ll see shortly that a virial or cubic equation of state is a better model and shows the two phase region.

Calculate the Number of Moles given a Pressure and Temperature

What if we wanted to calculate how many moles of gas are in the classroom?

One way is to create another function with the ideal gas law rearranged to return the number of moles of gas. Another way is to use the existing function and set the pressure to the room pressure and solve for the number of moles of gas given the room volume and temperature.

In keeping with our theme of introducing upcoming concepts when we’re learning about a related topic, we’ll introduce the fsolve method that we’ll learn more about in a couple weeks.

The molar volume of an ideal gas at 85 kPa and 295 K is 0.029 m3/mol
The number of moles of gas in the room is 26493
26493.050209974554

The lambda function defines the independent variable ‘mV’ and then uses that with the ‘pressure_IG’ function with a guess value of 1 for the molar volume. The lambda function sets the output of the pressure_IG function to the classroom pressure and the fsolve finds the value of mV that satisfies that condition (the pressure equaling the classroom pressure). The lambda function’s use in the fsolve is particularly useful when the function has more than one variable.

The pressure in a 1 Liter vessel with 1 mole of gas inside is 2494200 Pa
The moles of an ideal gas at 85 kPa and 295 K inside the classroom is 26493 mol

Great, we got the same answers as before for both scenarios. Let’s see if we don’t define enough variables.

Not enough information for calculation

Equation of State: van der Waals

The van der Waals equation of state is a more complex equation of state that accounts for non-ideal behavior of gases. The van der Waals equation of state is given by:

P=RTV~−b−aV~2P = \frac{RT}{\tilde{V} - b} - \frac{a}{\tilde{V}^2}

where aa and bb are constants that depend on the gas. The constants for several compounds are in the below table:

Compounda (L^2 bar/mol^2)b (L/mol)
oxygen1.3820.03186
methane2.2830.04278
water5.5360.03049
<Figure size 640x480 with 1 Axes>

Here in the log-log plot, the van der Waals EOS agrees with the ideal gas result at pressures below about 1000 kPa (100 bar). Above those pressues, the van der Waals predicts a significantly high pressure than the ideal gas law.

Classes and Objects

Classes and objects are used so frequently in the programming of commercial software. You’ll use objects (an instance of a class) when you write efficient code where you don’t have code blocks that repeat. In that way, classes are similar to functions.

Classes and objects are appropriate to use when have multiple similar scenarios that require similar definitions and methods. For example, you may have multiple tanks than each have a maximum capacity and whose current liquid mass varies depending on the geometry such as a cylindrical or square tank. You can define a class for a tank and then create objects for each tank. The class defines the attributes and methods for the tank and the objects are the individual tanks.

In the motivation lecture, we setup a class with properties and a method to easily specify different gas species with a method to calculate the ideal gas heat capacity with the properties of each gas object (instance). Here we’ll work with some tanks.

Example problem with tank objects

Problem Statement: There are 5 tanks that are spherical, 3 cylindrical tanks, and 4 conical tanks. The three spherical tanks have levels of 2.5, 3.0, 2.6, 3.2, and 3.5 m. The cylindrical tanks have levels of 1.5, 2.0, and 2.5 m. The conical tanks have levels of 1.0, 1.5, 2.0, and 2.5 ft. The tanks have a diameter of 2 m, except for the spherical tanks that have a diameter of 5 m. The tanks are filled with biodiesel. Calculate the total volume of fluid in the tanks for inventory purposes.

The total volume of fluid in the tanks is 228.33 m3
[tank(diameter=5, level=2.5, maxCapacity=1000, name='tank00002', shape='sphere'), tank(diameter=5, level=3, maxCapacity=1000, name='tank00003', shape='sphere'), tank(diameter=5, level=2.6, maxCapacity=1000, name='tank00004', shape='sphere'), tank(diameter=5, level=3.2, maxCapacity=1000, name='tank00005', shape='sphere'), tank(diameter=5, level=3.5, maxCapacity=1000, name='tank00006', shape='sphere')]
The level in the first spherical tank is 2.5 m

This example may seem complex, but using and being familiar with objects is important. You’ll work with objects all the time when you’re programming. A simple example is a list. Lists are objects that have methods. A method you can call on a python list is the append method. The append method adds an element to the end of the list.

[tank(diameter=5, level=2.5, maxCapacity=1000, name='tank00002', shape='sphere'), tank(diameter=5, level=3, maxCapacity=1000, name='tank00003', shape='sphere'), tank(diameter=5, level=2.6, maxCapacity=1000, name='tank00004', shape='sphere'), tank(diameter=5, level=3.2, maxCapacity=1000, name='tank00005', shape='sphere'), tank(diameter=5, level=3.5, maxCapacity=1000, name='tank00006', shape='sphere'), tank(diameter=5, level=2.8, maxCapacity=1000, name='tank00014', shape='sphere')]