All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
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…
Amol Patel
updated on 06 Sep 2021
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
CH4+2(O2+3.76N2)⇒CO2+2H2O+7.52N2
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
Xo2=(number of moles of O2)(number of total moles)=number of moles of O2number of moles of O2+number of moles of N2=22+7.52=0.21
similarly for nitrogen the mole fraction is
XN2=(number of moles of N2)(number of total moles)=number of moles of N2number of moles of O2+number of moles of N2=7.522+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 -
Leave a comment
Thanks for choosing to leave a comment. Please keep in mind that all the comments are moderated as per our comment policy, and your email will not be published for privacy reasons. Please leave a personal & meaningful conversation.
Other comments...
Week 6: Conjugate Heat Transfer Simulation
AIM- To simulate a Conjugate Heat Transfer flow through a pipe, while the inlet Reynolds number should be 7000. To run the grid independance test on 3 grids and show that the outlet temperature converges to a particular value To observe the effect of various supercycle stage interval on the total simulation time.…
09 Nov 2022 06:55 AM IST
Week 7: Shock tube simulation project
AIM - To set up a transient shock tube simulation Plot the pressure and temperature history in the entire domain Plot the cell count as a function of time SHOCK TUBE- The shock tube is an instrument used to replicate and direct blast waves at a sensor or a model in order to simulate actual explosions…
07 Nov 2022 09:18 PM IST
Week 5: Prandtl Meyer Shock problem
AIM - 1. To understand what is a shock wave. 2. To understand the what are the different boundary conditions used for a shock flow problems. 3. To understand the effect of SGS parameter on shock location. 4. To simulate Prandalt Meyer Shcok Wave. OBJECTIVE - Que 1. What is Shock Wave? A shock wave or shock,…
01 Nov 2022 06:36 PM IST
Week 4.2: Project - Transient simulation of flow over a throttle body
AIM - Transient simulation of flow over a throttle body. OBJECTIVE - Setup and run transient state simulation for flow over a throttle body. Post process the results and show pressure and velocity contours. Show the mesh (i.e surface with edges) Show the plots for pressure, velocity, mass flow rate and total…
12 Feb 2022 07:08 AM IST
Related Courses
0 Hours of Content
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.