Challenge

Week 4.1- Handling Mixtures with Cantera

AIM -

To understand Quantity Class in cantera and create various volume mixtures.

OBJECTIVE -

To use the "moles" method/function and calculate the number of moles for the air mixture.

To use the mass_fracction_dict() to calculate the mass fraction of various components of the mixture.

To use proper syntax for the mole fraction while feeding in the information in the python code.

To apply proper changes in the code to get the right value of AFT for the mixture.

INTRODUCTION -

The Class in python is something that can store a lot more information than a variable as we know the in variable only one type of infromation can be store . but in a class we can store different kind of infromation for the same thing

like if we use a variable of a car and if we want to use it store jsut the name a variabel is fine but if we want to store the name, selling_price, number of miles run by the car , current price after depreciation in cost and the depreciation factor for each car... we will be using Class for this case.

below is the example shown for a class

"""
	Object oriented programming OOP

	porgram to present the details of a car
"""

class Car:

	num_wheels = 4

	def __init__(self, car_name,car_price,depreciation_factor,features):
		self.name = car_name
		self.num_miles = 0
		self.selling_price = car_price
		self.current_price = self.selling_price
		self.depreciation_factor = depreciation_factor
		self.features = features #[False, False]
		print(self.name + " is a nice car")
		if self.features["AC"] is True : print ("enjoy your ac")
		if self.features["BT"] is True : print ("enjoy your bt")
		


	def drive(self, num_miles):
		self.num_miles = self.num_miles + num_miles
		self.current_price = self.selling_price - self.num_miles*self.depreciation_factor
		print("current price is :" + str(self.current_price))

	def add_ac(self):
		self.features["AC"] = True 
		self.selling_price = self.selling_price + 5000
		print("enjoy the AC")
	def add_bt(self):
		"""
			this fuction add BT and increases the cost
		"""
		self.features["BT"] = True
		self.selling_price = self.selling_price +1000
		print("enjoy the music with new bluetooth")

c1 = Car(car_name = "Honda",car_price = 100000, depreciation_factor = 0.01, features = {"AC":True,"BT":False})
c2 = Car("Hyundai",150000, 0.02,{"AC":False,"BT":True}) 


c1.drive(1000)
c2.drive(5000)


the outut for the above code is

if we add the following piece of code in the above example we get output information about the class as shown in the image below

help(c1)

Now we will be using the Quantity Class form cantera to create various volume mixtures

First we will create the mixture for air that can be useful for combustion of methane

as we know the chemical reaction of Methane

`CH_4 + 2(O_2 + 3.76 N_2) => CO_2 + 2H_2O + 7.52N_2`

so we need 2 moles of oxygen and 7.52 moles of nitrogen as they are in present in the molar ration of 1: 3.76 in air

now the mole fraction of oxygen is

`X _(o2) = sf"(number of moles of O2)"/sf "(number of total moles)" = sf "number of moles of O2"/(sf"number of moles of O2" + sf"number of moles of N2") = 2/(2+7.52) =0.21`

similarly for nitrogen the mole fraction is

`X _(N2) = sf"(number of moles of N2)"/sf "(number of total moles)" = sf "number of moles of N2"/(sf"number of moles of O2" + sf"number of moles of N2") = 7.52/(2+7.52) =0.7899 ~= 0.79`

Now we will use this values of mole faction to imput the proper amount of oxygen and nitrogen in the air mixture .

To calulate the number of moles of the air mixture we will first import cantera in our python code then using the Solutions Class we will set the gas in the code usign the gti30.cti file

then for the air mixture we use the Quantity class and set A as a Quantity(gas)

also we will set the temperature pressure at STP and to input the mole fraction we have the values calculated earlier so we will input the moles fraction in the form of dictioinary

the code until this steps is given below

"""
	Sample code to vary the temperautre of air and oxidizer individually
"""
import cantera as ct 

gas = ct.Solution("gri30.cti")

A = ct.Quantity(gas)
A.TPX = 298.15 , ct.one_atm, {"O2":0.21, "N2":0.79}

ANALYTICAL CALCULATIONS FOR MASS FRACTION -

From the stoichiometric equation we have

Number of moles of O2 = 2

and Number of moles of N2 = 7.52

the molar mass of O2 and N2 are 32 and 28 respectively

so the total mass of O2 = number of moles of O2 * molar mass of O2 = 2* 32 = 64

and the total mass of N2 = number of moles of N2 * molar mass of N2 = 7.52*28 = 210.56

so, the total weight of mixture = total mass of O2 + total mass of N2 = 64 + 210.56 = 274.56

now the mass fraction = total mass of the component / total mass of the mixture

mass fraction of O2 = total mass of O2 / total mass of mixture = 64/ 274.56 = 0.2333

mass frcation of N2 = total mass of N2 / total mass of mixture = 210.56/274.56 = 0.7668 ~= 0.77

Now to calculate the number of moles in 1 gram of air

we know the mass fraction of O2 and N2

so the mass of O2 in 1 gram of air = mass fraction of O2 * 1 gram = 0.2333 *1 = 0.2333

the mass of N2 in 1 gram of air = mass fraction of N2 * 1 gram = 0.7668 *1 = 0.7668

number of moles in 0.2333 g O2 = 0.2333/64 = 0.003645

number of moles in 0.7668 g N2 = 0.7668/28 = 0.027385

total number of moles = number of moles of O2 + number of moles of N2 = 0.003645+0.027385 = 0.03103

we write the code to see the results

"""
	Sample code to vary the temperautre of air and oxidizer individually
"""
import cantera as ct 

gas = ct.Solution("gri30.cti") # setting up the system as gas 

A = ct.Quantity(gas) # using the Quantity Class
A.TPX = 298.15 , ct.one_atm, {"O2":0.21, "N2":0.79} # inputting the temp ,pressure and mole fraction of the mixture
A.mass = 1# mass of air is 1 gram
print("number of moles in 1g of air = " + str(A.moles))

we get the following output

Now as we have calcualted the mass fraction analytically, we will be calculating the mass fraction of the components from the cantera-python code for that we will be using the mass_fraction_dict method

for this we will add the code line as shown below

print(A.mass_fraction_dict())

here this line of code means to print the mass_fraction_dict of the object A which in our case is the air mixture

so our whole code becomes

"""
	Sample code to vary the temperautre of air and oxidizer individually
"""
import cantera as ct 

gas = ct.Solution("gri30.cti") # setting up the system as gas 

A = ct.Quantity(gas) # using the Quantity Class
A.TPX = 298.15 , ct.one_atm, {"O2":0.21, "N2":0.79} # inputting the temp ,pressure and mole fraction of the mixture
A.mass = 1# mass of air is 1 gram
print("number of moles in 1g of air = " +str(A.moles))
print(A.mass_fraction_dict()) # to calculate the mass fraction of each component

the output for this code is shown below

Now we will create a new object B which is also a gas and this is basically our fuel methane

so then we aill be settign the object B by giving in the temperature pressure and mass fraction

using the following lines of code

# second object B is the fuel: methane
B =ct.Quantity(gas) # using the Quantity class
B.TPX = 298.15, ct.one_atm, 'CH4: 1546' # 1546 is useless as we have just a single component
B.moles = 1 # setting the moles of CH4 as 1 according to the stoichiometric reaction  

Now we have the second object our fuel generated

Here while writing the name of the component the number 1546 doesnt mean anything and it is useless also as we have just a single component we will be have its mole fraction as 1 so that is the reason we havent specified it in a way we did for object A as it was a mixture we used a dictionary to give the values of mole fraction of each component

we can see here in the following code the output of the mole fraction of B is 1

"""
	Sample code to vary the temperautre of air and oxidizer individually
"""
import cantera as ct 

gas = ct.Solution("gri30.cti") # setting up the system as gas 
# first object A is the air mixture containing O2 and N2
A = ct.Quantity(gas) # using the Quantity Class
A.TPX = 298.15 , ct.one_atm, {"O2":0.21, "N2":0.79} # inputting the temp ,pressure and mole fraction of the mixture
A.mass = 1# mass of air is 1 gram
print("number of moles in 1g of air = " +str(A.moles))
print(A.mass_fraction_dict()) # to calculate the mass fraction of each component
# second object B is the fuel: methane
B =ct.Quantity(gas) # using the Quantity class
B.TPX = 298.15, ct.one_atm, 'CH4: 1546' # 1546 is useless as we have just a single component
B.moles = 1 # setting the moles of CH4 as 1 according to the stoichiometric reaction  
print(B.mole_fraction_dict())

output:

Now its time for the comustion and calcualtion of the Adiabatic Flame Temperature for that we will be creating a new object M and it will be the mixture of A and B for that we will use the code line

M = A + B # adding the Objects A and B to create a mixture M

it will also help to apply right molar balance but for that we need to give proper values of moles in the main code in line 10 and 16

then using the equilibrate method we will do the combustion calculation to get the AFT

the main code is given below

"""
	Sample code to vary the temperautre of air and oxidizer individually
"""
import cantera as ct 

gas = ct.Solution("gri30.cti") # setting up the system as gas 
# first object A is the air mixture containing O2 and N2
A = ct.Quantity(gas) # using the Quantity Class
A.TPX = 298.15 , ct.one_atm, {"O2":0.21, "N2":0.79} # inputting the temp ,pressure and mole fraction of the mixture
A.mass = 1# mass of air is 1 gram
print("number of moles in 1g of air = " +str(A.moles))
print(A.mass_fraction_dict()) # to calculate the mass fraction of each component
# second object B is the fuel: methane
B =ct.Quantity(gas) # using the Quantity class
B.TPX = 298.15, ct.one_atm, 'CH4: 1546' # 1546 is useless as we have just a single component
B.moles = 1 # setting the moles of CH4 as 1 according to the stoichiometric reaction  
print(B.mole_fraction_dict())

# The following step is used to apply right molar balance
M = A+B
M.equilibrate('HP')
print(M.T)

the output of the above code is

here we can see the AFT value comes out to be about 347.14 K which is wrong because we havent set the proper value of the moles in mixture Object A so we need to go back to line numbr 10 and edit it so that the moles of A are set the proper value of the stoichioimetric chemical reaction

the total number of moles of O2 + N2 mixture in the stoichiometric reaction is 9.52 so now line number 10 becomes

A.moles = 9.52 # total number of moles of O2 and N2 in stoichiometric reaction

now the main code becomes

"""
	Sample code to vary the temperautre of air and oxidizer individually
"""
import cantera as ct 

gas = ct.Solution("gri30.cti") # setting up the system as gas 
# first object A is the air mixture containing O2 and N2
A = ct.Quantity(gas) # using the Quantity Class
A.TPX = 298.15 , ct.one_atm, {"O2":0.21, "N2":0.79} # inputting the temp ,pressure and mole fraction of the mixture
A.moles = 9.52 # total number of moles of O2 and N2 in stoichiometric reaction
print("mass of mixture object A = " +str(A.mass))
print(A.mass_fraction_dict()) # to calculate the mass fraction of each component
# second object B is the fuel: methane
B =ct.Quantity(gas) # using the Quantity class
B.TPX = 298.15, ct.one_atm, 'CH4: 1546' # 1546 is useless as we have just a single component
B.moles = 1 # setting the moles of CH4 as 1 according to the stoichiometric reaction  
print(B.mole_fraction_dict())

# The following step is used to apply right molar balance
M = A+B # adding objects A and B to create mixture object M 
M.equilibrate('HP') # combustion calcualtion at constant enthalpy and pressure 
print(M.T) # getting AFT value as output 

the output for the code :

now we can see that the AFT value is correct and also we changed out first output to the mass of A which we calculated in the analytical calculations part as the total mass of mixture coming out to be 274.56 which is quite close to the value we get as 274.65

CONCLUSIONS -

  1. We can see that the Quantity class in cantera is very useful to set the input values for any mixture that we need according to our convenience we can see the mass , moles or mass fraction or mole fraction for the mixture .
  2. We can calculate the unknow properties of the mixture very easily with the help of Quantity and the methods inside this class.
  3. Setting the appropiate value will only give us correct results or else the calculation will be useless to proper care is needed while inputiing the values.
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.