Assignment 07

Assignment 7 – Units

Assignment 7 uses the function in assignment 4 and adds units.

The Van Der Waals equation given at https://en.wikipedia.org/wiki/Van_der_Waals_equation is the full form of the well known equation PV = nRT or the ideal gas law. Where

P = Pressure of gas (atm, psi, in Hg)
V = Volume of gas (liters)
n = moles of gas
R = Universal gas constant (0.0821 liter-atm/mole-Kelvin)
T = Absolute temperature of the gas (Fahrenheit, Celsius, Kelvin)

Using this write a function that (named CalculateVolume) will Calculate the Volume of a gas in liters given the arguments P, n, and T (R is a constant and can be built into the function).

Prompt the user to input the values or directly enter them with units as a variable assignment and the units for Temperature and Pressure and output the Volume in liters. Show units on your output.

For a gas at 1 atmosphere on a normal temperature of 70 F (you can convert to K) what would be the volume of a mole of the gas?

Demonstrate the solution for the following inputs (using 1 mole of gas each time):

T = 500C, P = 1 atm
T = 100F, P = 30 in Hg
T = 500 K, P = 5 psi

Generate an engineering report that demonstrates the ability to accept input of a value and units and outputs the results with units. You can explore different methods of allowing the user to input units, and the program should prevent the user from being able to input invalid units.

Please note – you must use a units package, I know handling units in your own code is a possibility – but part of this assignment is about using packages to assist in technical calculations. 

Materials

Lecture 4 – https://youtu.be/Y0T5Ud4YZ0k –

Lecture 9 – https://youtu.be/q1OuvrUcbJ8

Ideal Gas Law – https://en.wikipedia.org/wiki/Ideal_gas_law – This may be useful to you, you should know this.

Of course you may want some code to play with (try this and code)

import pint;

# This is some code to make it easy select from a list 
# of choices
def GetInputFromArray(prompt, choices):
    print(prompt)
    for i in range(0, len(choices)):
        print("Enter {} for {}".format(i, choices[i]))
    selection = int(input("Enter Value: "))
    return choices[selection]

a = GetInput("Select Pressure", ["atm", "in Hg", "psi"])
print("You selected: {}".format(a))

# PINT will let you create a numeric value from this string

# This is just a value
v = 1.0

# We can create a unit register
from pint import UnitRegistry
ureg = UnitRegistry()

# Now it is a value with units
v = v * ureg.parse_expression(a)

print(" Here is the value with units {}".format(v))

# You can see this is the documentation https://pint.readthedocs.io/en/0.7.2/tutorial.html 
# and we can outut in a different set of units

print(v.to(ureg.atm))

# And we can even ask what set of units we are using
print(v.dimensionality)

It is worth noting that converting Temperatures in Pint is a little trickier than other units. This is because Temp Conversions between F and C also have a Delta (32 degrees) that other conversions do not have. You can easily handle this by a setting in pint

ureg = UnitRegistry(autoconvert_offset_to_baseunit = True)

This is documented fully at the site below. Note: you will have to read the docs to solve this problem. I specifically chose an example that requires you to get and apply some information from the documentation to solve.

PINT Documentation at ReadTheDocs – https://pint.readthedocs.io/en/stable/nonmult.html

Some good practices

There are plenty of ways to approach this problem, and I do give you code to read and understand. Never use code in your programs unless you have a decent understanding of the code. So one approach is to create a function that always returns the user input in the set of units you need for the calculations – here is an example of doing just that. This function allows the user to input the code in the units they have – but it returns the results in atmospheres. Note that this still returns a unitized number and you may just want the magnitude for your calculations in the units you need for the calculation. Try the code below;

def getPressureInAtm():
    print("You will input the pressure value and then select the units of the input")
    p = float(input("Enter pressure:"))
    choices = ["atm", "in Hg", "psi"]
    print("You will now select the units of the value entered")
    for i in range(0, len(choices)):
        print("Enter {} for {}".format(i, choices[i]))
    units = int(input("Enter Value: "))
    p1 = p * u.parse_expression(choices[units])
    return p1.to(u.atm)

print(getPressureInAtm())

Output

You should complete the following table as part of your output. The program does NOT have to produce this table, you should be able to run your program and complete the table.

Input
Temperature
Temp in KInput PressureP in atmVolume in liters
500 C1 atm
100 F30 in Hg
500 K5 psi