Python program to solve the otto cycle.
Aim:
Solve and plot the otto cycle by using Python.
OBJECTIVE:
THERE ARE TWO MAIN OBJECTIVE OF THIS PROJECT:
- To draw the p-v diagram of otto cycle
- To calculate the thermal efficiency.
GOVERNING EQUATIONS:
1.PV = RT
2. PVγ= Constant for adiabatic process
where P = pressure
V = volume γ = adiabatic index
3.volume change
where,
V = Volume chnge
4.Thermal efficiency
5.Swept volume
Vs = (π/4) * (bore^2) * (Stroke length)
6.clearance volume
Vc = swept volume/(CR-1)
where,
CR or r = compression ratio R = ratio of 'connecting rod length' and 'crank pin radius'
Otto cycle process:
Process 1-2 isentropic compression
Process 2-3 constant volume heat addition
Process 3-4 isentropic expansion
Process 4-1 constant volume heat rejection
PROGRAM OVERVIEW:
First of all,certain geometrical dimensions and some of the initial variable need to be defiend.After that, all other variables can be calculated by using some formulas and equation which then can be used to plot the p-v diagram and thermal efficiency of the cycle.
PROGRAM TO PLOT THE PV DIAGRAM FOR OTTO CYCLE AND CALCULATION OF THERMAL EFFICIENCY:
#program to plopt otto cycle
import math
import matplotlib.pyplot as plt
def engine_kinematics(bore,stroke,con_rod,cr,start_crank,end_crank):
#engine parameter for calculation of volume change
a = stroke/2 #crank pin radius
R = con_rod/a
#volume parametrs
vs = (math.pi/4)*pow(bore,2)*stroke
vc = vs/(cr-1)
sc = math.radians(start_crank)
se = math.radians(end_crank)
num_values = 50
dtheta = (se - sc)/num_values
V = []
for i in range(0,num_values):
theta = sc+i*dtheta
#volume change formula
#spliting the formula into terms
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))*vc)
return V
#inputs
p1 = 101325
t1 = 500
t3 = 2300
gamma = 1.4
#geometric parameters
bore = 0.1
stroke = 0.1
con_rod = 0.15
cr = 12
#swept and clearance volume
vs = (math.pi/4)*pow(bore,2)*stroke
vc = vs/(cr-1)
# variables at state 1
v1 = vs+vc
#variables at state2
v2 = vc
# p1v1^gamma = p2v2^gamma
p2 = p1*pow(v1,gamma)/pow(v2,gamma)
#p1v1/t1 = p2v2/t2 | p1v1/t1 = rhs | p2v2/t2 =rhs
rhs = p1*v1/t1
t2 = p2*v2/rhs
v_comp = engine_kinematics(bore,stroke,con_rod,cr,180,0)
constant = p1*pow(v1,gamma)
p_comp = []
for v in v_comp:
p_comp.append(constant/pow(v,gamma))
#variables at state 3
v3 = v2 #heat addition at conatant volume
# as volume is constant p2/t2 = p3/t3
p3 = p2*t2/t3
v_exp = engine_kinematics(bore,stroke,con_rod,cr,0,180)
constant = p3*pow(v3,gamma)
p_exp = []
for v in v_exp:
p_exp.append(constant/pow(v,gamma))
#variables at stste 4
v4 =v1
#p4v4^gamma = p3v3^gamma
p4 = p3*pow(v3,gamma)/pow(v4,gamma)
t4 = p4*v4/rhs #as rhs is same
Thermal_efficiency = 1-1/(pow(cr,(gamma-1)))
print(Thermal_efficiency)
#plotting the pv diagram
plt.plot(v_comp,p_comp)
plt.plot([v2,v3],[p2,p3])
plt.plot(v_exp,p_exp)
plt.plot([v4,v1],[p4,p1])
plt.title('PV Diagram',size =20)
plt.xlabel('volume')
plt.ylabel('pressure')
plt.show()
EXPLANATION OF CODE:
- First of all a function is created with the nameof engine kinematics which is used to calculate the volume change using the formula shown above.
- the formula is split into 3 different terms to ease the calculation.valume change is calculated for different crank angle.
- after that inputs and geeometric parameters are defiend to calculate the all the variabels at different state.
- after calculating the variables at state2 the above created function is called to calculate the real volume and pressure change during cmpression by settting the crank angle from 180 to 0.
- And, dafter 4 state volume and pressure change calculation is done during expansion by calling the function by setting the crank angle from0 to 180.
ERROR FACED:
Only typing errors were faced which were simply tackeled just by reading the error shown by python.
OUTPUT:
The plot for PV diagram:
Thermal efficiency:
the efficiency calculated is 56%