Otto cycle P-V diagram simulation using Python
Introduction:
Four stroke thermodynamic Otto cycle was invented by the German scientist Nicolaus August Otto in the year around 1862 .
Thermodynamic Otto cycle is considered to be one of the most commonly used practical cycles. Present days’ all four stroke internal combustion (IC) engines used in your gasoline cars, trucks, motorcycles, constructions machines etc work on the principle of four strokes of Otto cycle. The internal combustion engine working on the principle of Otto cycle is called Spark Ignition (SI) engine in which used gasoline as the fuel. The four strokes of the Otto cycle or four-stroke engine are: suction of fuel, compression of fuel, combustion and expansion of fuel and exhaust of burnt fuel.
Objective:
To determine the thermal efficiency of the otto cycle, and to plot the P-V diagram.
Inputs:
Bore= the diameter of the cylinder.=0.1
Stroke= the length of far the piston travels in the cylinder=0.1
Connecting rod = the length of the rod that connects the piston and the crank pin=0.15.
Compression ratio= the ratio of the maximum to minimum volume the engine= 9:1.
Process involved in otto cycle:
- 1-2, isentropic compression
- 2-3 constant volume heat addition
- 3-4 isentropic expansion
- 4-1 constant volume heat rejection
The function program for the otto cycle is:
#program for otto cycle simulator
#By Thippeswamy
import math
import matplotlib.pyplot as plt
def engine_kinematics(bore,stroke,con_rod,cr,start_crank,end_crank):
import math
a= stroke/2
R= con_rod/a
v_s = math.pi/4*pow(bore,2)*stroke
vc = v_s/(cr-1)
sc = math.radians(start_crank)
ec = math.radians(end_crank)
num_values = 50
dtheta = (ec-sc)/(num_values-1)
V = []
for i in range(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
#input
p1 = 101325
t1 = 500
gamma = 1.4
t3= 2300
#geometric parameters
bore = 0.1
stroke = 0.1
con_rod = 0.15
cr = 8
#volume calculation
v_s = (math.pi/4)*pow(bore,2)*stroke
v_c = v_s/(cr-1)
v1 = v_s+v_c
#state point 2
v2 = v_c
#p2v2^gamma = p1v1^gamma
p2 = p1*pow(v1,gamma)/pow(v2,gamma)
#p2v2/t2 = p1v1/t1 | rhs= p1v1/t1 | p2v2/t2 = rhs
rhs= p1*v1/t1
t2= p2*v2/rhs
V_compression = engine_kinematics(bore,stroke,con_rod,cr,0,180)
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
#p3v3/t3 = p2v2/t2 | rhs = p2v2/t2 | p3v3/t3 = rhs
rhs= p2*v2/t2
p3= rhs*t3/v3
V_expansion = engine_kinematics(bore,stroke,con_rod,cr,180,0)
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))
#statenpoint 4
v4 = v1
#p4v4^gamma = p3v3^gamma
p4 = p3*pow(v3,gamma)/pow(v4,gamma)
#p4v4/t4 = p3v3/t3 | rhs = p3v3/t3
rhs = p3*v3/t3
t4= p4*v4/rhs
#thermal efficiency of the otto cycle
thermal_eff = 1-1/pow(cr,(gamma-1))
thermal_eff = thermal_eff*100
print(\'Thermal Eff=\',thermal_eff)
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=\'Constant Volume Heat Rejection\')
plt.plot([v4,v1],[p4,p1],label=\'Isentropic Expansion\')
plt.legend(loc=\'best\')
plt.show()
Explanation of the code:
matplotlib.pyplot is imported to plot the P-V diagram of the otto cycle
inputs which are the variables at state point one of the otto cycle are taken.
bore ,stroke,connecting rod length,compression ration are defined which are geometric parameters of the engine
Initial condtion of the air fuel mixture is taken as
T1=300k
P1=101325pa
To solve a problem we should have the data of two state points hence , the temperature at state point 3 is given as t3=2300k
Here, swept volume and clearance volume is calculated to plot the P-V diagram .
Determining the State variables at state point 2. The formula used in this step is
`p2*v2^γ=p1*v1 ^γ``(p1v1)/(t1)=(p2v2)/(t2)`
determining the state variables at state point 3 the formula used in this step is :
in this step volume v2 is equal to the volume v3 hence
v3=v2
`(p3v3)/(t3)=(p2v2)/(t2)`
Determining State variables at state point at state point 4. The formulae used in this step are .
In this state point volume v4 is equal to the volume v1 hence
v4=v1
`p4v4^gamma=p3v3^gamma`
Thermal efficiency of the otto cycle is
`ղ=1-1/(cr^(γ-1 ) )`
Plot of the above otto cycle program is :
Errors Faced :
common typographical mistakes
missing of colon operator in for loop
Steps to over come those Errors:
patient while typing
make sure to use colon operator in for loop so that i can get indentation also