Project

PYTHON CODE TO SOLVE AN OTTO CYCLE AND CALCULATE THE THERMAL EFFICIENCY

OBJECTIVE

To write a python code to solve the otto cycle and plot the P-V diagram of the otto cycle and displaying the efficiency of the cycle as an output.

CODE

import matplotlib.pyplot as plt
import numpy as np
import math


def engine_kinematics(bore, stroke, con_rod, rc, start_crank, end_crank):

    piston_radius = stroke/2
    r = con_rod/piston_radius
    v_s = (math.pi/4)*pow(bore, 2)*stroke
    v_c = v_s/(rc-1)
    v = []
    theta = np.linspace(start_crank, end_crank, 100)

    for i in range(0, 100):
        term1 = 0.5*(rc-1)
        term2 = r+1-math.cos(math.radians(theta[i]))
        term3 = (r**2) - pow(math.sin(math.radians(theta[i])), 2)
        term3 = pow(term3, 0.5)
        v.append((1 + term1*(term2 - term3))*v_c)

    return v

# Inputs


gamma = 1.4


# State Variables
p1 = 101325
t1 = 500
t3 = 2300

# Engine geometric parameters
bore = 0.1
stroke = 0.1
con_rod = 0.15
rc = 12

# Calculating the swept volume and clearance volume
v_swept = (math.pi/4)*pow(bore, 2)*stroke
v_clearance = v_swept/(rc-1)
v1 = v_swept + v_clearance
v2 = v_clearance

# State variables at point 2
# p2v2^gamma = p1v1^gamma
# p2 = p1*((v1/v2)^gamma)= p1*(rc^gamma)
p2 = p1*pow(rc, gamma)

# v1/v2 = (t2/t1)^(1/(gamma-1)) | t2 = t1*(v1/v2)^(gamma-1)
t2 = t1*pow(rc, (gamma-1))

# State variables at state point 3
# constant volume heat addition(2-3) hence v2 = v3
v3 = v2

# p3v3/t3 = p2v2/t2 | p3 = p2*v2*t3/(t2*v3)
p3 = p2*t3/t2

# State variables at state point 4
# constant volume heat rejection(4-1) hence v4 = v1
v4 = v1

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

# adiabatic expansion (3-4) t3/t4 = (v4/v3)^(gamma-1)
t4 = t3/pow(rc, (gamma-1))

# For drawing curves process (1-2) ADIABATIC COMPRESSION
p_compression = []
constant_c1 = p1*(v1**gamma)
v_compression = engine_kinematics(bore, stroke, con_rod, rc, 180, 0)
for i in v_compression:
    p_compression.append(constant_c1/pow(i, gamma))

# For drawing curves process (3-4) ADIABATIC EXPANSION
p_expansion = []
constant_c2 = p3*(v3**gamma)
v_expansion = engine_kinematics(bore, stroke, con_rod, rc, 0, 180)
for i in v_expansion:
    p_expansion.append(constant_c2/(pow(i, gamma)))

# Efficiency of the engine
thermal_efficiency = 1-(1/pow(rc, (gamma-1)))

# plotting the curves
plt.figure(1)
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.xlabel(\'volume\')
plt.ylabel(\'pressure\')
plt.show()

print(\'The thermal efficiency of the given engine is \',thermal_efficiency)

EXPLANATION OF THE CODE FOR THE FUNCTION \'engine_kinematics\'

To have the variation of volume during the combustion, the function \'engine_kinematics\' is created.

In this function the relation between the cylinder volume and clearance volume is used to calculate an array of values between the starting and end crank angles of the engine given as input.

Here R is the ratio of length of connecting rod and piston radius.

The function needs the inputs (bore,stroke,con_rod,rc,start_crank,end_crank) and it gives the volume(v) as the output.

By using this function the v_compression at (180-0) and v_expansion at (0-180) are calculated.

REST OF THE CODE EXPLANATION

Firstly, the state variables p1,t1,t3 are given as input values for the cycle.

Then the engine geometric parameters are defined. From these values the swept and clearance volumes ae calculated. \'rc\' is the compression ratio of the engine

By using the P-V relationship in Adiabatic process, P*V^gamma = c

and P-V-T general relationship for a gas, (P*V)/T = c, the state variables at all the four points are calculated.

But to draw the curves (1-2) and (3-4), the variation of cylinder volume is needed during the compression and expansion process.

For that the function engine_kinematics is created and used in the code.

Then by using the p-v relationships in adiabatic process, p_compression and p_expansion are also calculated.

In the process steps (2-3) and (4-1) the volume is constant hence a straight line is alone drawn.

In the plot the points of four states are drawn and the curves connecting them also is drawn.

At the end the thermal efficiency of the engine is displayed as the output

The otto cycle plot is shown below.

CONCLUSION

  • PYTHON CODE FOR SOLVING THE OTTO CYCLE IS WRITTEN.
  • THE OTTO CYCLE IS PLOTTED AND CURVES ARE APPROPRIATLEY DRWAN.
  • THERMAL EFFICIENCY OF THE ENGINE IS GIVEN AS OUTPUT.
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.