All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
Aim: To write a code for Otto cycle and extract the p-v plot and efficiency of otto cycle using python script. Theory: Internal Combustio Engine OTTO cycle: 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…
D MAHESH
updated on 11 Apr 2021
Aim:
To write a code for Otto cycle and extract the p-v plot and efficiency of otto cycle using python script.
Theory:
Internal Combustio Engine OTTO cycle:
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:
Efficiency of ideal otto cycle:
The starting point is the general expression for the thermal efficiency of a cycle:
The convention, as previously, is that heat exchange is positive if heat is flowing into the system or engine, so is negative. The heat absorbed occurs during combustion when the spark occurs, roughly at constant volume. The heat absorbed can be related to the temperature change from state 2 to state 3 as:
![]() |
![]() |
|
![]() |
The heat rejected is given by (for a perfect gas with constant specific heats)
Substituting the expressions for the heat absorbed and rejected in the expression for thermal efficiency yields
We can simplify the above expression using the fact that the processes from 1 to 2 and from 3 to 4 are isentropic:
The quantity is called the compression ratio. In terms of compression ratio, the efficiency of an ideal Otto cycle is:
The ideal Otto cycle efficiency is shown as a function of the compression ratio in Figure 3.11. As the compression ratio, , increases,
increases, but so does
. If
is too high, the mixture will ignite without a spark (at the wrong location in the cycle).
Procedure:
1. Open sublime python script editor and save the file as otto cycle and first write the commands for the importing the math and plot.
2. Now define the engine parameters using 'def' command and calculate the Volume of the cylinder and append the volume up to 50 values. Here vvolume formulae is split into three terms and then calculated.
3. Now define the input parameters like pressure, temperature and air constant gamma and also define the compression ratio, stroke length, bore diameter and connecting rod length.
4. After defining all input parameters now start calculting the pressure, volume and temperature and write the code in the format of code.
5. To calculate pressure and volume at the compression and expansion process write a 'for' loop function and append the pressure with respective to volume at the compression and expansion.
6. Now write the command to calculate the thermal efficiency of this cycle.
7. Now write the commands to plot the p-v diagram and mention legends at x-axis and y-axis.
8. Now save the file again and run the whole script and we get the plot and thermal efficiency.
Code and Results:
#otto cycle simulator
#Engine kinematics
#By Mahesh
#import
import math
import matplotlib.pyplot as plt
def engine_kinematics(bore,stroke,conn_rod,c_r,start_crank,end_crank):
#Engine parameters
a = stroke/2
R = conn_rod/a
c_r = 10
#Volume Calculations
v_s = (math.pi/4)*pow(bore,2)*stroke
v_c = v_s/(c_r-1)
#strating crank angle and ending crank angle
sc = math.radians(start_crank)
ec = math.radians(end_crank)
num_values = 50
dtheta = (ec-sc)/(num_values-1)
V =[]
#write the 'for' loop command to calculate volume
for i in range(0,num_values-1):
theta = sc + i*dtheta
term1 = 0.5*(c_r-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
#intial inputs of pressure,temperature and value of isentropic expansion factor gamma
p1 = 101325
t1 = 500
gamma = 1.4
t3 = 2300
#engine parameters
bore = 0.15
stroke = 0.2
conn_rod = 0.8
c_r = 10
#volume calculations for swept volume and compression volume
v_s = (math.pi/4)*pow(bore,2)*stroke
v_c = v_s/(c_r-1)
v1 = v_c + v_s
#constant volume process
v2 = v_c
#from isentropic process p2v2^gamma = p1v1^gamma
p2 = p1*pow(v1,gamma)/pow(v2,gamma)
#to find temperature value at 2nd use ideal gas equation
rhs = p1*v1/t1
t2 = p2*v2/rhs
V_compression = engine_kinematics(bore,stroke,conn_rod,c_r,180,0)
constant = p1*pow(v1,gamma)
P_compression = []
for v in V_compression:
P_compression.append(constant/pow(v,gamma))
#at stage 3
v3 = v2
rhs_1 = p2*v2/t2
p3 = t3*rhs_1/v3
V_expansion = engine_kinematics(bore,stroke,conn_rod,c_r,0,180)
constant = p3*pow(v3,gamma)
P_expansion = []
for v in V_expansion:
P_expansion.append(constant/pow(v,gamma))
#at stage 4
v4 = v1
p4 = p3*pow(v3,gamma)/(pow(v4,gamma))
#final temperature
rhs_2 = p3*v3/t3
t4 = p4*v4/rhs_2
#calcuate efficiency of otto cycle = n
n = 1 - (1/pow((c_r),(gamma -1)))
print(n)
#write the commands for plotting
plt.plot(V_compression,P_compression)
plt.plot([v2,v3,],[p2,p3])
plt.plot(V_expansion,P_expansion)
plt.plot([v4,v1],[p4,p1])
plt.ylim([-500000,8000000])
plt.xlim([0,0.005])
plt.ylabel('Pressure')
plt.xlabel('Volume')
plt.title('Otto Cycle')
plt.grid()
plt.show()
Thermal efficiency of this cycle is 60% and p-v diagram of otto cycle is plotted perfectly.
Conclusion:
Thus otto cycle thermal efficiecy and p-v diagram is plotted using the python script and sublime script editor.
Leave a comment
Thanks for choosing to leave a comment. Please keep in mind that all the comments are moderated as per our comment policy, and your email will not be published for privacy reasons. Please leave a personal & meaningful conversation.
Other comments...
Week 1 Spur Gear Challenge
AIM: To do analysis on the two gears with three different materials and compare their results with the help of ANSYS structural analysis. Procedure: 1. Open ANSYS structural analysis and create three types of materials and assign properties to those materials. 2. Now import the geomentry of gears to the ansys and…
24 Apr 2021 02:33 AM IST
Week 1 Stress Concentration on a Plate with hole
AIM: To perform structural analysis on steel plate and compare the stress results of steel plate with one hole and steel plate with two holes using the ANSYS Workbench. Procedure: 1. Open the Ansys and select the structural analysis tab and drag it to the work space. 2. Now open the space claim to model the geomentry and…
23 Apr 2021 11:51 PM IST
Week 3 - Solving second order ODEs
Aim: To write a code for Simple Pendulum with help of ordinary differential equation in the format of python using Sublime. Theory: A pendulum is a body suspended from a fixed support so that it swings freely back and forth under the influence of gravity. When a pendulum is displaced sideways from its resting,…
19 Apr 2021 03:13 AM IST
Week 2 Air standard Cycle
Aim: To write a code for Otto cycle and extract the p-v plot and efficiency of otto cycle using python script. Theory: Internal Combustio Engine OTTO cycle: 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…
11 Apr 2021 04:44 PM IST
Related Courses
0 Hours of Content
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.