Piston Kinematics simulation for Otto cycle in Python
Objective:
- To write a code which creates a PV diagram for otto cycle in Python.
- To find the thermal efficiency of the engine.
Introduction:
The Otto cycle was given by Dr. Nikolaus August Otto. It is a gas power cycle that is used in a Spark Ignition(i.e. petrol engine) for its working. The entire modern petrol engine works on the Otto cycle.
It has four processes:
1. Isentropic (reversible adiabatic) compression
2. Constant volume (Isochoric) heat addition
3. Isentropic (reversible adiabatic) Expansion
4. Constant volume heat rejection.
1. Process 1-2: Isentropic Compression
This process involves the motion of piston from TDC to BDC. The air that is sucked into cylinder during suction stroke undergoes reversible adiabatic (isentropic) compression. Since the air is compressed, the pressure increases from P1 to P2, the volume decreases from V1 to V2, temperature rises from T1 to T2, and entropy remains constant.
2. Process 2-3: Constant Volume Heat Addition
This process is an isochoric process i.e. the heat is added to the air at constant volume. The piston in this process rest for a moment at TDC and during this time heat is added to the air through external source. Due to the heat addition, the pressure increases from P2 to P3, pressure, volume remains constant(i.e. V2=V3), temperature increases from T2 to T3 and entropy increases from S2 to S3.
3. Process 3-4: Isentropic Expansion
In this process, the isentropic (reversible adiabatic) expansion of air takes place. The piston moves from TDC to BDC. Power is obtained in this process which is used to do some work. Since this process involves expansion of air, so the pressure decreases from P3 to P4, volume increases from V3 to V4, temperature falls from T3 to T4 and entropy remains unchanged (i.e. S3=S4).
4. Constant Volume Heat Rejection
In this process, the piston rest for a moment at BDC and rejection of heat takes place at constant volume. The pressure decreases from P4 to P1, Volume remains constant (i.e. V4=V1), temperature falls from T4 to T1.
When this cycle is used in four stroke petrol engine than two process increases. one is suction of air fuel mixture inside the cylinder which takes place at constant atmospheric pressure and other one is exhaust of gases out of the engine cylinder at constant atmospheric pressure. These two process are not shown in ideal otto cycle that we have discussed above.
Code:
# Piston kinematics for otto cycle
import math
import matplotlib.pyplot as plt
def engine_kinematics(bore,stroke,conn_rod,cr,c_v,s_v,start_crank,end_crank):
a = stroke/2
R = conn_rod/a
sc = math.radians(start_crank)
ec = math.radians(end_crank)
num_values = 50
d_theta = (ec-sc)/(num_values-1)
V = []
for i in range(0,num_values):
theta = sc+i*d_theta
\'\'\'
Volume trace for isentropic process
(1+(0.5*(cr-1).*(R+1-cosd(theta)-(R^2-sind(theta).^2).^0.5)))*c_v;
\'\'\'
term1 = 0.5*(cr-1)
term2 = R + 1-math.cos(theta)
term3 = pow(R,2)-pow(math.sin(theta),2)
term4 = pow(term3,0.5)
V.append(((1+term1*(term2-term4)))*c_v)
return V
# input parameters
t1 = 500
p1 = 101325
gamma = 1.4
t3 = 2300
# cylinder geometry
bore = 0.5
stroke = 0.5
conn_rod = 0.6
cr = 8
s_v = math.pi*(1/4)*pow(bore,2)*stroke
c_v = s_v/(cr-1)
v1 = s_v+c_v
# state point two
v2 = c_v
# p1v1^gamma = p2v2^gamma
p2 = p1*pow((v1/v2),gamma)
V_compression = engine_kinematics(bore,stroke,conn_rod,cr,c_v,s_v,180,0)
constant = p1*pow(v1,gamma)
P_compression = []
for v in V_compression:
P_compression.append(constant/pow(v,gamma))
# ideal gas law| p2v2/t2 = p1v1/t1
rhs = p1*v1/t1
t2 = p2*v2/rhs
# state point 3
v3 = v2
# ideal gas law|p3v3/t3 = p2v2/t2
lhs = p2*v2/t2
p3 = t3*lhs/v3
V_expansion = engine_kinematics(bore,stroke,conn_rod,cr,c_v,s_v,0,180)
constant = p3*pow(v3,gamma)
P_expansion = []
for v in V_expansion:
P_expansion.append(constant/pow(v,gamma))
# state point 4
# p3v3^gamma = p4v4^gamma
v4 = v1
p4 = p3*pow((v3/v4),gamma)
# p4v4/t4 = p1v1/t1
t4 = (p4*v4)/(p1*v1/t1)
# plotting
plt.plot([v2,v3],[p2,p3],label=\'Constant volume heat addition\')
plt.plot(V_compression,P_compression,label=\'Isentropic Compression\')
plt.plot(V_expansion,P_expansion,label=\'Isentropic Expansion\')
plt.plot([v4,v1],[p4,p1],label=\'Constant volume heat rejection\')
plt.legend()
plt.title(\'Piston Kinematics for Otto Cycle\')
plt.show()
Thermal_efficiency = (1-(1/pow(cr,gamma-1)))*100
print(Thermal_efficiency)
Explanation:
Libraries are imported such as \'math\' for mathematical calculation and \'matplotlib.pyplot\' for plotting.
1. First of we have determined all the inputs for the geometric parameter such as bore,connecting rod,stroke,gamma etc.
2. The stroke volume and clearance volume is found by the formula.
3. The inputs are given for point no. 1 and also inputs are found out for all 4 points with the help of relations and formula.
4. Now, here comes the main part, if we want to trace the isentropic expansion and compression we have to use the formula :
\'\'V = (1+(0.5*(cr-1).*(R+1-cosd(theta)-(R^2-sind(theta).^2).^0.5)))*c_v\".
5. And the pressure at that movement can be traced by the following formula: P_ = C./V_expansion.^gamma;
6. In order to create a function in Python we have to use the command \'\' def name_of_function( All inputs)\'\'. In this case we have created a function for piston kinematics and traced the path of isentropic processes using the formula mentioned in pt.4 & 5
7. In our code we have called our function command to trace a plot from point 1 to 2 and from point 3 to 4.
8. We have done our plotting and called the plot command as plt. Legend is assigned to the plot and title is given.
9. Thermal efficiency is calculated by the formula :(1-(1/pow(cr,gamma-1)))*100 in Python which is equal to 56.4724%.
Plot: