Week 2 Air standard Cycle
#Aim:Write code that can solve an otto cycle and make plots for it using Python.
Otto cycle
Otto cycle is the ideal cycle for sparkignition reciprocating engines.Named after Nikolaus A. Otto, who built a successful four-stroke engine in 1876 in Germany.Can be executed in two or four strokes.
1.)Four stroke: Intake, compression, power and exhaust stroke
2.)Two stroke: Compression and power strokes.
Otto cycle consists of four processes:
– Isentropic compression (1-2)
– Isochoric (constant volume) heat addition (2-3)
– Isentropic expansion (3-4)
– Isochoric (constant volume) heat rejection (4-1)
All the processes are internally reversible.



#The minimum volume formed in the cylinder when the piston is at TDC is called the clearance volume.
#The volume displaced by the piston as it moves between TDC and BDC is called the displacement volume.
# The ratio of the maximum volume formed in the cylinder to the minimum (clearance) volume is called the compression ratio, r of the engine:
#Efficiency of Otto cycle:

----------------------------------------------------******************---------------------------------------
"""
Air standard Otto cycle simulator program
"""
"""
Equation for relation between crank angle and Volume inside combustion chamber
V/V_c=1+1/2(comp_ratio-1)[R+1-cos(theta)-(R^2-sin^2(theta))^(1/2)]
"""
import math
import matplotlib.pyplot as plt
#Geometric parameters
Bore=0.1
Stroke=0.1
Con_rod=0.15
comp_ratio=12
#Volume computation
V_s=(math.pi/4)*pow(Bore,2)*Stroke
V_c=V_s/(comp_ratio-1)
def engine_kinematics(Bore,Stroke,Con_rod,comp_ratio,Swept_volume,Clearance_volume,start_crank,end_crank):
"""
Function takes Bore,Stroke,Con_rod,comp_ratio,Swept_volume,Clearance_volume,start_crank,end_crank and
return Volume of combustion chamber for every crank angle
"""
a=Stroke/2 #Radius of crank pin
R=Con_rod/a #Constant defined in above equation
#Start and End crank angle
sc=math.radians(start_crank)
ec=math.radians(end_crank)
#No of values between Start and End crank angle
num_values=50
dtheta=(ec-sc)/(num_values-1)
#List defined to store values of Volume for each and every crank angle
V=[]
for i in range(0,num_values):
theta=sc+i*dtheta
#From above equation
term1=0.5*(comp_ratio-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 at point 1
p1=101325
t1=500
gamma=1.4
#Temp at point 3
t3=2300
v1=V_c+V_s
#STate input at point 2
v2=V_c
"""
Since compression is isentropic between points 1 and 2
So
p1v1^gamma=p2v2^gamma
"""
p2=p1*pow(v1,gamma)/pow(v2,gamma)
#p1v1/t1=p2v2/t2| t2=(p2v2)/(p1v1/t1)
t2=(p2*v2)/(p1*v1/t1)
V_compression=engine_kinematics(Bore,Stroke,Con_rod,comp_ratio,V_s,V_c,180,0)
constant=p1*pow(v1,gamma)
P_compression=[]
for j in V_compression:
P_compression.append(constant/pow(j,gamma))
#State variables at point 3
v3=v2
#p3v3/t3=p2v2/t2 |p3=(p2v2/t2)/(v3/t3)
p3=(p2*v2/t2)/(v3/t3)
V_expansion=engine_kinematics(Bore,Stroke,Con_rod,comp_ratio,V_s,V_c,0,180)
constant=p3*pow(v3,gamma)
P_expansion=[]
for k in V_expansion:
P_expansion.append(constant/pow(k,gamma))
#State variables at point 4
v4=v1
#p3v3^gamma=p4v4^gamma
p4=(p3*pow(v3,gamma)/(pow(v4,gamma)))
#p3v3/t3=p4v4/t4 |t4=(p4v4)/(p3v3/t3)
t4=(p4*v4)/(p3*v3/t3)
#Combustion Efficiency
Otto_cycle_efficiency=1-(1/pow(comp_ratio,gamma-1))
print(Otto_cycle_efficiency)
fig=plt.figure()
ax=fig.add_subplot()
ax.annotate('1', xy=(v1,p1),
fontsize=9)
ax.annotate('2', xy=(v2,p2),
fontsize=9)
ax.annotate('3', xy=(v3,p3),
fontsize=9)
ax.annotate('4', xy=(v4,p4),
fontsize=9)
plt.plot(V_compression,P_compression,color='green',label='1-2:Compression')
plt.plot(V_compression[24],P_compression[24],'<',color='orange')
plt.plot([v2,v3],[p2,p3],color='green',label='2-3:Heat Addition')
plt.plot(v2,(p2+p3)/2,'^',color='orange')
plt.plot(V_expansion,P_expansion,color='green',label='3-4:Expansion')
plt.plot(V_expansion[24],P_expansion[24],'>',color='orange')
plt.plot([v4,v1],[p4,p1],color='green',label='4-1:Heat Rejection')
plt.plot(v4,(p4+p1)/2,'v',color='orange')
plt.legend(loc='upper right')
plt.show()
----------------------------------------------*****************----------------------------------------------
#Using only Isentropic and Isochoric relationship for finding values of state variables Volume,Pressure,Temperature at certain point ,plot given below would be obtained.Which is linear but actually Compression and Expansion processes are parabolic in nature.So this plot is incorrect only state variables value at end of cycle is correct.

-------------------------------------------******************-----------------------------------------------
#Using this equation:
`V/V_c=1+1/2(comp.ratio-1)[R+1-cos(theta)-(R^2-sin^2(theta))^(1/2)]`
that is giving volume of combustion chamber for every crank angle predict compression and expansion curve accurately .

#Conclusion:
1.PV diagram plotted represent actual Otto cycle PV plot.
2.Due to compression, Pressure at the end of compression stroke has increased and volume decreased.
3.Then due to heat addition at constant volume,only pressure increased.
4.Again due to expansion ,Pressure decreased and Volume increased.
5.At last,Heat rejection take place at constant volume,so pressure decreases.
--------------------------------------------------*********************-------------------------------------