Challenge

Solving Air standard Cycle using Python

AIM – Solving the Otto Cycle and plotting the P-V diagram.

Objective –

1. To write a code that can solve an Otto-cycle.

2. To write a code to create a PV diagram.

3. To calculate the efficiency of the Otto-cycle.

Theory –

An Air-standard Otto Cycle is an idealized thermodynamic cycle that describes the functioning of a typical Spark Ignition (SI) engine. It consists of four processes - constant volume heat addition (combustion), constant volume heat rejection, isentropic compression and isentropic expansion.

Following assumptions are used in air-standard Otto cycle –

  1. Air is the working fluid and it behaves as the ideal gas. (CP , CV and = constant)
  2. All the processes are internally reversible.
  3. Combustion process is constant volume heat addition.
  4. Exhaust process is constant volume heat rejection.

The actual p–V diagram for an actual cycle has rounded corners because of the processes of combustion take place at a finite rate. The ideal Otto cycle has sharp corners because the ‘combustion’ is switched on and off instantaneously.

Problem Statement –

The main problem that we faced during the programming of the code was how to plot the diagram for the compression and expansion process. If we simply used the ‘plot’ command, the processes are plotted as a straight line which is wrong. Hence we defined the function and then later called that function to plot the required graph.

Equations used –

  • VS = pi/4*bore2*stroke;
  • VC = VS/(rc – 1);
  • V1 = V4 = VC + VS ;
  • V2 = V3 = VC;
  • P1 V1 / T1 = P2 V2 / T2;
  • P * Vγ = c;
  • (T2/T1) = (P2/P1)^(γ-1/) = (V1/V2)^( γ-1) = rc^( γ-1);
  • a = stroke/2;
  • R = con_rod/a;
  • Ƞ = 1 – (1/rc)(γ-1)
  • V/Vc = 1 + 0.5(cr-1) * [R+1-cos(θ) - {R2-(sin(θ))2}0.5]

where,

VS = Stroke Volume

VC = Clearance Volume

rc = Compression Ratio

γ = Adiabatic Index

θ = Crank Angle

Ƞ = Efficiency of Otto-cycle

P = Pressure at various points

T = Temprature at various points

V = Volume at various points.

Code –

"""
otto cycle simulator
"""
import math
import matplotlib.pyplot as plt

def engine_kinematics (bore, stroke, con_rod, cr, start_crank, end_crank):
	# Geometric Parameters of cylinder
	a = stroke/2
	R = con_rod/a

	# Volume specification
	V_s = (math.pi/4)*pow(bore,2)*stroke
	V_c = V_s/(cr-1)

	# V/V_c = 1 + 0.5(cr-1)*[R+1-cos(theta) - (R^2 - (sin(theta))^2)^0.5]
	sc = math.radians(start_crank)
	ec = math.radians(end_crank)

	V = []

	num_values = 100
	dtheta = (ec - sc)/(num_values - 1)

	for i in range (0,num_values):
		theta = sc + i*dtheta
		term1 = 0.5*(cr-1)
		term2 = R + 1 - math.cos(theta)
		term3 = pow(pow(R,2)-pow(math.sin(theta),2),0.5)

		V.append((1 + term1*(term2-term3))*V_c)
	return V


# Inputs
p1 = 101325
T1 = 500
gamma = 1.4
T3 = 2300

# Geometric Parameters of cylinder
bore = 0.1
stroke = 0.1
con_rod = 0.15
cr =12

# Volume specification
v_s = (math.pi/4)*pow(bore,2)*stroke
v_c = v_s/(cr-1)

"""
State point 1
"""
v1 = v_c + v_s

"""
State point 2
"""
v2 = v_c

# p2*v2^gamma = p1*v1^gamma
p2 = p1*pow(v1,gamma)/pow(v2,gamma)

# p2*v2/T2 = p1*v1/T1 | p1*v1/T1 = rhs (gas constant)
rhs = p1*v1/T1
T2 = p2*v2/rhs
constant1 = p1*pow(v1,gamma)

v_compression = engine_kinematics(bore, stroke, con_rod,cr, 180, 0)

p_compression = []
for v in v_compression:
	p_compression.append(constant1/pow(v,gamma))

"""
State point 3
"""
v3 = v2

# p3/T3 = p2/T2
p3 = p2*T3/T2

"""
State point 4
"""
v4 = v1

# p4*v4^gamma = p3*v3^gamma
p4 = p3*pow(v3,gamma)/pow(v4,gamma)

# p4/T4 = p1/T1
T4 = p4*T1/p1
constant2 = p3*pow(v3,gamma)
v_expansion = engine_kinematics(bore, stroke, con_rod,cr, 0, 180)

p_expansion = []
for v in v_expansion:
	p_expansion.append(constant2/pow(v,gamma))

# Efficiency = 1 - (1/cr)^(gamma-1)
efficiency = 1 - pow(1/cr,gamma-1)
print(efficiency)

# 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()

Solution Procedure –

First we import the ‘math’ library for basic math functions and ‘matplotlib.pyplot’ library as plt for plotting purposes.

Then we defined the values of the inputs such as p1, T1, T3 and gamma. Cylinder dimension were also specified such as bore, stroke, connecting rod and compression ratio.

Then we calculate the volume specification or the stroke volume and the clearance volume.

After that, we calculate the pressure, temperature and volume at each end state and write the formula for efficiency calculation.

Then we use the ‘plot’ command. The plot thus obtained is –

But this plot is wrong as the compression and expansion processes are isentropic hence the graph for them wouldn’t be the straight lines.

Hence we used the calling the function. We define the function ‘engine_kinematics’ with inputs as bore, stroke, con_rod, cr, start_crank, end_crank.

To calculate the value of ‘V’ as an isentropic variable, we used the formula

V/Vc = 1 + 0.5(cr-1) * [R+1-cos(θ) - {R2-(sin(θ))2}0.5]

with the ‘append’ command inside a ‘for’ loop of length 100. ‘return’ command is given to return the value of ‘V’.

While trying to calculate the volume during the compression and expansion process, we call the function ‘engine_kinematics’. And then we calculate the value of pressure during these process with the formula

P * Vγ = c

After using the plot command, for each process, we got the correct PV diagram.

Result –

Efficiency –

Ƞ = 62.989 %

P-V Diagram –

Discussion -

This concludes the study of Otto-cycle. But similar program can be used for any air-standard cycle in which the compression and expansion is isentropic or reversible adiabatic as the compression and expansion curve will follow the same path. For example, Diesel cycle, Brayton cycle etc.

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.