Python code for air standard cycle - Otto cycle
Project objective: To write a program in Python to solve an Otto cycle and make plots for it.
Project challenges:
- To solve and find out different state variables in the Otto cycle and plot its p-V diagram.
- To calculate the thermal efficiency of the given Otto cycle.
Otto cycle theory:




The Otto cycle is a set of processes used by spark ignition internal combustion engines (2-stroke or 4-stroke cycles). These engines a) ingest a mixture of fuel and air, b) compress it, c) cause it to react, thus effectively adding heat through converting chemical energy into thermal energy, d) expand the combustion products, and then e) eject the combustion products and replace them with a new charge of fuel and air. The different processes are shown in Figure.
- Intake stroke, gasoline vapor and air drawn into engine (5 to 1).
- Compression stroke, p , T increase (1 to 2).
- Combustion (spark), short time, essentially constant volume (2 to 3). Model: heat absorbed from a series of reservoirs at temperatures T2to T3.
- Power stroke: expansion (3 to 4).
- Valve exhaust: valve opens, gas escapes.
- (4 to 1). Model: rejection of heat to series of reservoirs at temperatures T4to T1.
- Exhaust stroke, piston pushes remaining combustion products out of chamber (1 to 5).
Otto cycle governing equations:
Compression ratio (rc) = (Total cylinder volume )/(Clearance volume)
Compression ratio (rc) = (Swept volume + Clearance volume)/(Clearance volume)
Compression ratio (rc) = (v_s + v_c)/v_c
Compression ratio (rc) = v_1/v_2 or v_3/v_4
Swept volume (Vs) = (π/4)⋅D2⋅L
where D is bore diameter and L is stroke length,
Clearance volume (Vc) = v_s/((r_c-1)`
After calculation of all this equation we put the plot accordingly. The code for whole process is given as below.
= v/v_c = 1 + 1/2(r_c-1) [R+1−cosθ−〖(R^2-〖Sin〗^2 θ)〗^(1/2)`
where R = length of connecting rod/crank radius ,
Adiabatic process equation = pvγ = Constant
Ideal Gas equation = pv = mRT
Thermal efficiency of Otto cycle(η)=
Input conditions :
- Gamma = 1.4
- Initial conditions, such as temperature (T1)and pressure (p1) of the surrounding is assumed to be 373K and 101.325 kPa respectively.
- The geometrical design of the engine cylinder which consists of bore diameter, stroke length, and length of connecting rod is also known and accordingly vsand vc are calculated.
- Bore = 0.1
- Stroke = 0.1
- Connecting rod length = 0.15
- Compression ratio = 12
- The maximum temperature (T3) occurring inside the cylinder is also assumed as T3 = 2358K
- Other state variables are calculated using Adiabatic processes equations and ideal gas equations. Similarly, the thermal efficiency is also calculated accordingly using the formula given as above
- For different values of crank angle for which the program will iterate is 100, and accordingly, the V array is set as null and after every iteration using for loop, a new value will be stored as an array using 'append' command and will be returned back after receiving desired values.
Input Python code:
# Air standard cycle (Otto Cycle)
import matplotlib.pyplot as plt
import math
import numpy as np
# Otto cycle piston kinematics equation
def piston_kinematic_equation(bore,stroke,connecting_rod_length,CR,start_crank,end_crank):
# Otto engine paremeters (Assumed)
a = stroke/2
R = connecting_rod_length/a
# Calculating swept volume & clearance volume
V_swept = (math.pi/4) * (bore**2) * stroke
V_clearance = V_swept / (CR-1)
# Crank angle
SC = math.radians(start_crank)
EC = math.radians(end_crank)
theta = np.linspace(SC,EC,100)
V = []
for i in range(0,100):
THETA = theta[i]
# Otto cycle process equation
term1 = 0.5 * (CR - 1)
term2 = R + 1 - math.cos(THETA)
term3 = (R*2 - (math.sin(THETA)2))*0.5
# Final equation
v = (1 + term1 * (term2 - term3))*V_clearance
V.append(v)
return V
# Input variables (given)
# Specific Heat Ratio
gamma = 1.4
# Temperature at State-3
T3 = 2358
# Pressure & Temperature at state-1
P1 = 101325
T1 = 373
#Engine geometric parameters (Assumptions)
bore = 0.1
stroke = 0.1
connecting_rod_length = 0.15
CR = 12
#Calculating swept volume & Clearance volume
V_swept = (math.pi/4) * (bore**2) * stroke
V_clearance = V_swept / (CR-1)
V1 = V_swept + V_clearance
V2 = V_clearance
# State-2
# P1 * V1^gamma = P2 * V2^gamma
# V1 / V2 = gamma
P2 = P1 * (CR**gamma)
# P1*V1/T1 = P2*V2/T2
T2 = (P2 * V2 * T1)/(P1 * V1)
constant_c = P1*(V1**gamma)
V_compression = piston_kinematic_equation(bore,stroke,connecting_rod_length,CR,180,0)
P_compression = []
for i in V_compression:
P_compression.append(constant_c/i**gamma)
# State-3
V3 = V2
# P2*V2/T2 = P3*V3/T3
P3 = (P2 * T3) / T2
constant_e = P3*(V3**gamma)
V_expansion = piston_kinematic_equation(bore,stroke,connecting_rod_length,CR,0,180)
P_expansion = []
for j in V_expansion:
P_expansion.append(constant_e/j**gamma)
# State-4
V4 = V1
# P3 * V3^gamma = P4 * V4^gamma
P4 = P3 * ((V3/V4)**gamma)
# Thermal Efficiency
eta = 1 - (1/(CR**(gamma-1)))
efficiency_in_percent = eta*100
print("Thermal Efficiency of Otto Cycle =",efficiency_in_percent)
# p-V diagram plotting
plt.plot(V_compression,P_compression)
plt.plot(V_expansion,P_expansion)
plt.plot([V2,V3],[P2,P3])
plt.plot([V4,V1],[P4,P1])
plt.title("p-V Diagram of Otto Cycle",fontweight="bold")
plt.xlabel("Volume (m^3)")
plt.ylabel("Pressure (Pa)")
plt.show()
Output:
The thermal efficiency calculated, and the program written is as follows:
* Remote Interpreter Reinitialized *
Thermal Efficiency of Otto Cycle = 62.98928275128466
>>>
p-V diagram of the Otto cycle for given engine parameters at a different crank angle is found to be as

NOTATIONS :
- p = pressure,
- V = volume of engine cylinder,
- T = temperature,
- Vc= clearance volume, (Clearance volume is a volume between the cylinder head and the piston top when the piston is at top dead center (TDC).),
- Vs= swept volume,(Swept volume is the displacement of one cylinder. It is the volume between top dead center (TDC) and bottom dead center (BDC).),
- γ= specific heat,
- D = bore diameter (diameter of each cylinder)
- L = stroke length (The stroke length is how far the piston travels in the cylinder, which is determined by the cranks on the crankshaft.)
- η= thermal efficiency,