PYTHON-generating otto cycle graph and calculating thermal efficiency
Objective: Writing a code in python which can solve the Otto cycle as well as make plots for it.
- making a P-V diagram of the cycle.
- calculating the thermal efficiency of the cycle.
Introduction:
The air-standard-Otto cycle is the idealized cycle for the spark-ignition internal combustion engines. This cycle is shown in the p-v diagram in the results tab. The Otto cycle 1-2-3-4 consists of the following four processes:
- Process 1-2: Reversible adiabatic compression of air.
- Process 2-3: Heat addition at constant volume.
- Process 3-4: Reversible adiabatic expansion of air.
- Process 4-1: Heat rejection at constant volume.
The isentropic processes are governed by the following equation mentioned in the tutorial :
`v = 1 + 1/2*(cr-1)*(R+ 1 - cos(theta) - (R^2 - sin(theta)^2)^(1/2)*(v_c))``
since this is a large equation, it was broken into three different terms and used in the code. Also in order to calculate the thermal efficiency of the Otto cycle, the following formula was used :
`eta_(th) = (1 - ((t_4-t_1)/(t_3-t_2)))*100`
Also, the above isentropic process equation was implemented in the code using the function engine_kinematics. The function was defined first and was later called for the compression and expansion processes respectively. All other necessary data has been provided in the code itself
#Otto cycle using engine kinematics
#importing necessary libraries
import math
import matplotlib.pyplot as plt
#defining the function for engine kinematics
def engine_kinematics(bore,stroke,con_rod,cr,start_crank,end_crank):
#geometric parameters of the engine
bore = 0.10
stroke = 0.10
con_rod = 0.15
cr = 12
a = stroke/2
r = con_rod/a
#volume parameters
v_s = math.pi*(1/4)*pow(bore,2)*stroke
v_c = v_s/(cr-1)
sc = math.radians(start_crank)
ec = math.radians(end_crank)
#number of data points to be taken for the varying angle
num_values = 50
#calculating theta
dtheta = (ec-sc)/(num_values-1)
#creating an empty array
v=[]
#calculating theta in the array
for i in range(0,num_values):
theta = sc + i*dtheta
term1 = 0.5*(cr-1)
term2 = r + 1 - math.cos(theta)
term3 = pow(r,2) - pow(math.sin(theta),2)
term3 = pow(term3, 0.5)
v.append((1 + term1*(term2 - term3))*v_c)
return v
#main function
#inputs
p1 = 101325
t1 = 500
gamma = 1.4
t3 = 2300
#geometric parameters
bore = 0.1
stroke = 0.1
con_rod = 0.15
cr = 12
#volume parameters
v_s = math.pi*(1/4)*pow(bore,2)*stroke
v_c = v_s/(cr-1)
v1 = v_c + v_s
#state point 2
v2 = v_c
#PV^gamma= constant
p2 = p1*pow(v1,gamma)/pow(v2,gamma)
rhs = p1*v1/t1
t2 = p2*v2/rhs
#calling the function defined above
v_compression = engine_kinematics(bore,stroke,con_rod,cr,180,0)
constant = p1*pow(v1,gamma)
#p_compression*v_compression^gamma = constant
p_compression = []
for v in v_compression:
p_compression.append(constant/pow(v,gamma))
#state point 3
v3 = v2
rhs = p2*v2/t2
p3 = rhs*t3/v3
v_expansion = engine_kinematics(bore,stroke,con_rod,cr,0,180)
constant = p3*pow(v3,gamma)
#p_compression*v_compression^gamma = constant
p_expansion = []
for v in v_expansion:
p_expansion.append(constant/pow(v,gamma))
#state point 4
v4=v1
p4 = p3*pow(v3,gamma)/pow(v4,gamma)
t4 = p4*v4/rhs
#calculating thermal efficiency using temperatures
thermal_efficiency = (1 - ((t4-t1)/(t3-t2)))*100
print(thermal_efficiency)
#Plotting all the stages individually
plt.plot([v2,v3],[p2,p3])
plt.plot(v_compression,p_compression)
plt.plot(v_expansion,p_expansion)
plt.plot([v4,v1],[p4,p1])
plt.title("Otto cycle")
plt.xlabel("Volume")
plt.ylabel("Pressure")
plt.show()
Results:
- P-V diagram for the Otto-cycle:
- The thermal efficiency of the Otto-cycle:
62.98~63%
References: