Scripting and Plotting of a P-V diagram of an Otto Cycle using Python
Objective: Write code that can solve an otto cycle and make plots for it.
Explaination:
As in an ideal Diesel cycle four distinct processes are also followed in the Otto cycle: reversible adiabatic compression, addition of heat at constant volume, reversible adiabatic expansion, and rejection of heat at constant volume as follows:
- Air enters the cycle at point 1 when compression starts and it ends at point 2. The air is compressed in an isentropic compression through a volume ratio v1/v2, known as the compression ratio rc (the ratio of the volume of the working fluid before the compression process to its volume after).
- Heat addition starts at point 2 and ends at point 3. A quantity of heat Qinis added at constant volume, and the working fluid temperature rises.
- The isentropic expansion process starts at point 3 and it ends at point 4 to the original volume.
- The heat Qout is rejected at constant volume from point 4 to point 1. Air gets cooled and the working fluid temperature goes down at a constant volume until the cycle is completed.
PROCEDURE:
- Consider some given data:
p1 = 101325 pa
T1 = 500 k
T3 = 2300 k
gamma = 1.4
Stroke length = 0.1 m
Bore dia. = 0.1 m
connecting rod = 0.15 m
compression ratio (cr = 12)
Crankpin radius = stroke length/2
R = constant = con_rod/crankpin radius
2. Some Formulas used:
Swept vol. (v_s) = π/4 d2L
Clearance vol. (v_c) = swept vol./(cr -1)
V = v_c + v_s
p1v1/t1 = p2v2/t2 | Ideal gas equation
V/Vc= 1 + 1 (rc - 1)[R + 1 - cos θ - (R² – sin² θ)1/2]
This eq. used to describe a relation between crank angle & vol. inside the combustion chamber. By this
eq. we get volume trace and we need this to finish our program.
Coding:
# otto cyle simulator
import math
import matplotlib.pyplot as plt
def engine_kinematics(bore, stroke, con_rod, cr, start_crank, end_crank):
#geometric expressions
a = stroke/2
R = con_rod/a
# different volume paramters
V_s= (math.pi/4)*pow(bore,2)*stroke
V_c = V_s/(cr-1)
sc = math.radians(start_crank)
ec = math.radians(end_crank)
num_values = 30
dtheta = (ec-sc)/(num_values-1)
V= []
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
p1 = 101325 # in pascal = 1 atm
t1 = 500 # in kelvin
t3 = 2300
gamma = 1.4
# engine parameters
bore = 0.1
stroke = 0.1
con_rod = 0.15
cr = 7
V_s= (math.pi/4)*pow(bore,2)*stroke
V_c = V_s/(cr-1)
v1 = V_s + V_c
v2 = V_c #state point 2
#isentropic relation
#p2v2/t2 = p1v1/t1 | rhs = p1v1/t1 | p1v2/t2 = rhs | t2 = p2v2/rhs
# p2v2^gamma = p1v1^gamma
p2 = p1*pow(v1,gamma)/pow(v2,gamma)
rhs = p1*v1/t1 #ideal gas law
t2 = p2*v2/rhs
V_compression = engine_kinematics(bore, stroke, con_rod, cr, 180, 0)
constant = p1*pow(v1,gamma)
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 | p3 = rhs*t3/v3
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_expansion = []
for v in V_expansion:
P_expansion.append(constant/pow(v,gamma))
# state point 4
v4 = v1
#p4v4^gamma = p3v3^gamma
p4 = p3*pow(v3,gamma)/pow(v4,gamma)
# p4v4/t4 = rhs
t4 = p4*v4/rhs
thermal_efficiency = 1-((t4-t1)/(t3-t2))
print(thermal_efficiency)
plt.plot([v2,v3],[p2,p3])
plt.plot([v4,v1],[p4,p1])
plt.plot(V_compression,P_compression)
plt.plot(V_expansion,P_expansion)
plt.xlabel('Volume')
plt.ylabel('Pressure')
plt.savefig('pv_diagram')
plt.show()
Thermal efficiency of this program: 0.5408434500405659
Note that the thermal efficiency of otto cycle is a function of compression ratio r and the ratio of specific
heats. As gamma is assumed to be constant for any working fluid, the efficiency is increased by
increasing the compression ratio. Further, the effeciency is independent of heat supplied and pressure
ratio. The use of gases with higher gamma values would increase efficiency of otto cycle.
Conclusion:
In depth use of functions, for loop gave good understanding of it.
with the help of this program we can observe the results at any given crank angle.