Project

Otto Cycle Plot Generator Using Python Function

Otto Cycle

An Otto cycle is an idealized thermodynamic cycle that describes the functioning of a typical petrol or spark-ignition piston engine (Otto engine). The Otto cycle is a description of what happens to a mass of gas as it is subjected to changes in pressure, temperature, volume, addition, and removal of heat. In the case of the Otto cycle, the effect will be to produce enough net work from the system so as to propel an automobile and its occupants in the environment.

An Otto Engine is essentially a 4-stroke engine. The four strokes are Intake, Compression, Expansion and Exhaust.

The Otto cycle has two pairs:

a pair of two parallel isentropic processes.

a pair of parallel constant volume processes.

Source: https://en.wikipedia.org/wiki/Otto_cycle

Process:

0-1: (Intake Stroke) This process describes the motion of the piston from the Top Dead Centre (TDC) to the Bottom Dead Centre (BDC). In this process, the engine breathes in the Air-Fuel (AF) mixture. Consequently, the state point 1 corresponds to temperature T1, pressure P1, and volume V1. Volume V1 is the capacity of the engine or the volume of the cylinder.

1-2: (Compression Stroke) This process describes the isentropic compression of the AF mixture. In the process, the temperature and the pressure raise to T2 and P2 respectively, while the volume decreases to V2. Volume V2 is called the clearance volume. The piston moves from BDC to TDC during the compression stroke.

Thus, the volume that the piston sweeps:

`V_(swept) = pi/4 B^2s`; where B is the bore and s is the stroke for the engine.

The clearance volume, `V_(clr)= V_(swept) /(Cr -1)`; where Cr is the compression ratio for the engine.

This follows from the fact that

`V_(cyl) = V_(swept) + V_(clr)` ; where Vcyl is the cylinder volume (V1)

`Cr = V_(cyl)/V_(clr)`; the Average value of Cr in an Otto engine is 8:1

2-3: (Ignition) This process describes the isochoric, constant volume heat addition process. Combustion of AF takes place during this process. The temperature and pressure rise to T3 and P3, while the volume remains the same, i.e., V3=V2. The piston remains at the TDC during ignition.

3-4: (Expansion Stroke) This process describes the isentropic expansion of the AF mixture. The temperature and pressure drop to T4 and P4, while the volume becomes V4, which is equal to V1. This stroke is also called a power stroke. The piston moves from the RDC to the BDC in the expansion stroke.

4-1: This process describes the isochoric heat rejection process. The state conditions reach back to the state 1. The piston remains at the BDC during this process.

1-0: (Exhaust Stroke) This process describes the motion of the piston from the BDC to TDC. The combusted gases are expelled from the engine during this process.

The cycle repeats through 0123410 curves.

Python Code For Otto Cycle Plot

Necessity:

The two isochoric processes are straight lines parallel to the pressure axes and thus, easy to plot. The values for the state point 3 can be calculated by the ideal gas equation (for one mole):

`PV = RT`, Where R is the gas constant

Or, `(P_aV_a)/T_a = (P_bV_b)/T_b`; where 'a', 'b' represent two states.

In order to code for an Otto cycle in Python, we need engine kinematic parameters which will give the volume at any position of the piston. The equation that gives the volume inside the cylinder at a particular crank angle is given by the Slider-Crank model. [link: https://www.engr.colostate.edu/~allan/thermo/page2/page2.html]

`V/V_(swept) = 1/(Cr -1) + [1+R-cos(theta)-(R^2 - sin^2(theta))^(1/2)]`

Or, `V = V_(swept)[1/(Cr -1) + [1+R-cos(theta)-(R^2 - sin^2(theta))^(1/2)]]`

Where,

`R=2 l_(con rod)/(stroke)`, `l_(conrod)` is the length of the connecting rod, `theta` is in radians

The Volume relation is needed especially for the two isentropic processes of the Otto cycle. Now, with the above formula, it is easy to get all the required volume values between the state points 1-2 and 3-4, we can easily code for the corresponding pressures as well. This is possible because for an isentropic process we have the adiabatic equation:

`P_aV_a^(gamma) = P_bV_b^(gamma)`;

Or, `T_aV_a^(gamma-1) = T_bV_b^(gamma-1)`

Or, `P_a^(1-gamma)T_a^(gamma) = P_b^(1-gamma)T_b^(gamma)`

Where `gamma` is the adiabatic constant and 'a', 'b' represent two state points.

Program

The program which I have designed make use of a function. The syntax for a function in Python is as:

def function-name(Parameter list):

statements, i.e. the function body

return Outputs/Results

Where Parameter list is a set of zero or more input arguments. Statements describe the coding that the function needs to perform for the parameters. The return statement contains zero or more outputs or results that the function is needed for.

1) Function Code

The code for the Parametric volume and pressure calculation is:

##Function For The Parametric Calculation of Volume and Pressure For An Otto Cycle


import numpy as N


def Otto_para(bore, stroke, cn_rd, cr, v_swept, s_c, e_c, p, v, G):


	
	R=2*(cn_rd/stroke)
	

	nm = 100                  #number of state points required  
	sc = N.radians(s_c)       #conversion to radians
	ec = N.radians(e_c)
	
	T = N.linspace(sc,ec,nm)  #creation of line space for a curve

	# Piston Cylinder Volume Calculation

	V=[] #preallocation

	for i in range(nm):

		#Slider_Crank Model Equation

		term1= 1/(cr-1)
		term2= 1 + R - N.cos(T[i])
		term3= (pow(R,2) - pow(N.sin(T[i]),2))
		term4 = pow(term3,0.5)
		V.append((term1 + 0.5*(term2-term4))*v_swept)  
		#append concatenates the values and stores them in an array

	# Piston Cylinder Pressure Calculation

	P =[] #preallocation

	for j in V:
		P.append(p*pow(v,G)/pow(j,G))


	return V,P     #returns the V and P array to the calling function

The above function, i.e., Ottopara takes in the input arguments: bore, stroke, length of the connecting rod, swept volume, starting crank angle(degrees), ending crank angle(degrees), pressure, volume, gamma (adiabatic constant). The outputs are Volume array, V, followed by the pressure array, P.

The module "numpy" helps in various mathematical functioning. In the above program, I used it to convert degrees to radians [see line 15: sc=N.radians(s_c)], and to create a line space [see line 18].

Calculation of volumes for the 1-2 and 3-4 processes follows the Slider_Crank model and the calculation of pressures for the same processes follows the adiabatic equation.

2) Main Program Code

The main program utilises two modules viz 'matplotlib.python' for MatLab like plotting and 'numpy' for scientific computing functions. To import Otto_para function, the syntax is: from 'filename' import 'function name'.

All the details are given in the code. However, it is worthy to mention that when using the function, the angle inputs for starting crank angle and ending crank angel are 180 degrees 0 degrees respectively in case of isentropic compression This is because when the compression is about to start, the piston is at the BDC, which corresponds to a crank angle of 180 degrees. The crank angle at the TDC is 0 degrees. Similarly, for the isentropic expansion, the angles for the function are 0 degrees and 180 degrees.

Code:

###Program to Plot an Otto Cycle 

##1)importing Modules
import matplotlib.pyplot as MP               #used for plotting like MatLab 
import numpy as N                            #used for scientific computing
from otto_funct import Otto_para as engine   #importing a function from a file

##2) Inputs
# Engine Dimensions

bore   = 0.1    #m
stroke = 0.1    #m
cn_rd  = 0.15   #m
cr     = 11


#Engine Volumes
v_swept = (N.pi/4)*pow(bore,2)*stroke 
v_clr   = v_swept/(cr-1)
v_cylinder = v_swept + v_clr


# Predifined State Inputs

G  = 1.4        
P1 = 101325  #Pascals, 1 atmosphere
T1 = 500     #K
T3 = 2300    #K

##3) State Point Calculations

#State 1
v1 = v_cylinder
p1 = P1
t1 = T1

''' Now we have got p1 and t1'''

# Stare 2
''' This state is achieved by the isentropic compression. Adiabatic Equation follows'''
v2 = v_clr
[V_comp, P_comp] = engine(bore, stroke,cn_rd,cr, v_swept,180,0,p1,v1,G) 
p2 = P_comp[-1]
t2 = t1*pow(cr,(G-1))

#State 3
'''An isochoric process. Ideal gas equation follows'''

t3 = T3
v3 = v2
p3 = (p2*v2*t3)/(v3*t2)


#State 4
'''This state is achieved by the isentropic expansion. Adiabatic Equation follows'''

v4 = v1
[V_expa, P_expa] = engine(bore, stroke,cn_rd,cr, v_swept,0,180,p3,v3,G)
p4 = P_expa[-1]
t4 = t3/pow(cr,(G-1))


##4) Efficiency
Effy = 100(1 - 1/pow(cr,G-1))    
Eff  = N.floor(100*Effy)               #Extracting the significant digits only
Ef   = Eff/100                         #Efficiency in percentage


##5) Plotting

MP.plot(V_comp,P_comp,[v2,v3],[p2,p3],V_expa,P_expa,[v4,v1],[p4,p1])     #plots the four processess
MP.xlabel('Volume in m^3')                                               #Adds X axis label
MP.ylabel('Pressure in N/m^2')                                           #Adds Y axis label
MP.title('Otto Cycle P-V Plot at Cr = ' + str(cr) +';  Efficiency = ' +str(Ef) +' %')                       #Adds the figure title
MP.legend(['1-2 Isentropic Compression', '2-3 Isochoric Heat Addition', '3-4 Isentropic Expansion','4-1 Isochoric Heat Removal'])
'''legend adds the curve names'''
MP.show()

Output:

The figure output from the above program is shown below.

Otto Cycle Plot In Python at Compression Ratio 8

The compression ratio for Otto cycle has an average value from 7 to 9. And the upper limit is 11. The efficiency of an Otto cycle is given by the following equation:

`eta %= 100[1 - 1/(Cr^(gamma-1))]`

The plots of the Otto cycles at compression ratios 7, 9 and 11 are given below:

Otto Cycle Plot In Python at Compression Ratio 7
Otto Cycle Plot In Python at Compression Ratio 9
Otto Cycle Plot In Python at Compression Ratio 11

From the above plots, it is quite clear that the efficiency of the Otto cycle increases as the compression ratio increases. However, it is not practical to construct an Otto engine whose compression ratio can exceed 8.

***END***

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.