Week 2 Air standard Cycle
Otto cycle plotter
Objective
Build a Python script that maps the pressure–volume loop of an ideal Otto‑cycle engine.
Problem statement
Write a Python code to plot the process curve of a petrol engine
Solution procedure
Code:
import matplotlib.pyplot as plt
import math
# input values
p1=101325
t1=500
bore=.1
stroke=.1
con_rod=.15
cr=12 #compression ratio
gamma=1.4
t3=2300
t=100
v_s=(math.pi/4)*pow(bore,2)*stroke
v_c=v_s/(cr-1)
#function for plotting the isentropic compression and expansion
def isentropic_plot(stroke,con_rod,v_c,theta_start,theta_end,no_theta):
theta_start=math.radians(theta_start)
theta_end=math.radians(theta_end)
v=[]
d_theta=(theta_end-theta_start)/(no_theta-1)
for i in range(0, no_theta):
theta=theta_start+i*d_theta
a=stroke/2
R=con_rod/a
item1=1/2*(cr-1)
item2=pow(R,2)-pow(math.sin(theta),2)
item3=R+1-math.cos(theta)-pow(item2,.5)
v.append((1+item1*item3)*v_c)
return v
# initial condition
v1=v_s+v_c
ideal_gas_const=p1*v1/t1
# properties at the end of isentropic compression (0 to 180)
v2=v_c
p2=pow(v1/v2,gamma)*p1
t2=p2*v2/ideal_gas_const
#list of v and p
# properties at the end of constant volume heat addition (2-3)
v3=v2
p3=ideal_gas_const*t3/v3
#list of v and p
# isentropic expansion (3-4) (180 to 0)
v4=v1
p4=pow(v3/v4,gamma)*p3
t4=p4*v4/ideal_gas_const
#list of v and p
# isentropic compression path
p_compression=[]
v_compression=isentropic_plot(stroke,con_rod,v_c,180,0,t)
for v in v_compression:
p_compression.append(pow(v3/v,gamma)*p3)
# isentropic expansion path
p_expansion=[]
v_expansion=isentropic_plot(stroke,con_rod,v_c,0,180,t)
for v in v_expansion:
p_expansion.append(pow(v1/v,gamma)*p1)
plt.plot(v_compression,p_compression)
plt.plot(v_expansion,p_expansion)
plt.plot([v1,v4],[p1,p4])
plt.plot([v3,v2],[p3,p2])
plt.show()
- First, I imported all the necessary modules
- Then I assign all the constant values
- Then I defined a function that will help me to plot the isentropic process curve. This function will be used later in the script.
- Now I have all the state 1 conditions.ns
- With the help of the ideal gas equation and PV^gamma =constant (isentropic process equation), all other state parameters are calculated.
- Then, at last, using the isentropic_plot function that I have defined earlier, I found the values of volume for isentropic processes, and with these values, I found the corresponding pressure value.
- with this isenropic pressure and volume value and state properties and using the plot function I ploted the Pv diagram.
Results
When the code is run, the plot for the Otto cycle is plotted.
(Plot is uploaded as a file)
Discussion
Such a script helps to visualize the processes of Ic engine