Python Challenge - Air standard Cycle
Air Standard Cycle Project
Aim - Write code that can solve an otto cycle and make plots for it.
Here are the requirements
- Your code should create a PV diagram
- You should output the thermal efficiency of the engine.
Solution:
The thermodynamic cycle we are going to write code is used in an internal combustion engine. The four-stroke Otto cycle is made up of the following four internally reversible processes:
- 1-2 isentropic compression
- 2-3 constant volume heat addition
- 3-4 isentropic expansion
- 4-1 constant volume heat rejection

We will use some basic thermodynamic formulas in our code and save the output than from using those outputs we will plot a graph.
Formulas are
Ideal Gas equation: PV=nRT
Adiabatic equation: PV^gamma = constant
Swept Volume = pi*(bore/2)^2*stroke
Compression Ratio Formula: CR = (Vsw +Vcl) /Vcl
This code can be used for petrol or diesel engine by changing the parameters.
Code:
import math
import matplotlib.pyplot as plt
#Defining the function
def engine_kinematics(bore,stroke,con_rod,cr,start_crank,end_crank):
#Geomatric parameter
a = stroke/2
R = con_rod/a
#Swept volume
v_s = (math.pi/4)*pow(bore,2)*stroke
#clearence volume
v_c = 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(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)
term4 = pow(term3,0.5)
V.append((1 + term1*(term2-term4))*v_c)
return V
#Inputs
p1 = 10135
t1 = 500
t3 = 2300
gamma = 1.4
#Geomatric parameter
bore = 0.1
stroke = 0.1
con_rod = 0.15
cr = 8
'''
Volume computation
'''
#Swept volume
v_s = (math.pi/4)*pow(bore,2)*stroke
#clearence volume
v_c = v_s/(cr-1)
v1 = v_s + v_c
#State point 2
v2 = v_c
p2 = p1*pow(v1,gamma)/pow(v2,gamma) #by iscentropic process pv^gamma = constant
rhs = p1*v1/t1
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
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
p4 = p3*pow(v3,gamma)/pow(v4,gamma) #by iscentropic process pv^gamma = constant
t4 = p4*v4/rhs
#Thermal Efficiency
n = (1 - pow(cr,1-gamma))*100
print('Thermal efficiency is',n)
#Plotting
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.show()
Code Explanation:
Givens
- Input the values of Gamma and Temp. of point 3
- Define state variables for point 1
- Define engine geometrics
Calculating
- Defining the function and the parameters it has to take.
- Calculating the swept volume and clearance volume by using formula and the given parameters.
- Defining the number of points for which we will calculate the values of Volume for each angle/position of the crank.
- For calculating volume at each position we will use for loop, we have broken the volume formula into 3 terms (term1,term2 & term4), the final volume will be (1+term1*(term2-term4)). Now we will store the respective values in the volume array will we have created earlier by using the append command and finally return the value of ‘V’.
- Calculating Pressure and volume at point 2 by using Adiabatic Equation and ideal gas equation.
- Calculating V_compression and P_compression by calling function (defining the crank angle 180-0) as we know the compression will take place from 180 to 0-degree range.
- As it's an isentropic process so constant = P1*V1^gamma.
- Calculating Pressure and volume at point 3 by using V3=V2(as the process takes place at constant volume) and the ideal gas equation.
- Calculating V_expansion and P_ expansion by calling function (defining the crank angle 0-180) and using constant = P3*V3^gamma.
- Calculating Pressure and volume at point 4 by using V4=V1(as the process takes place at constant volume) and the ideal gas equation.
- Now, calculating thermal efficiency by: (1-(1/cr^(gamma-1)))*100
- At last, plotting the variables.
Errors faced while
Some typo errors only.
Result:
- Thermal Efficiency – 56.47 %

- Plots
