
"""
otto_cycle
1-2 isentropic compression
2-3 constant volume heat addition
3-4 isentroppic expansion
4-1 constant volume heat rejection
"""
import math
import matplotlib.pyplot as plt
#inputs
p1=101325
t1=500
t3=2300
#geometric parameters
bore =0.1
stroke =0.1
conn_rod=0.15
r=12
gama=1.4
#volume computation
v_s = math.pi*0.25*pow(bore,2)*stroke #swept volume of cylinder
v_c= v_s/(r-1) #clearance volume
#function called
def engine_kinematics(bore, stroke, conn_rod, r, start_cra, end_cra):
a=stroke/2
R=conn_rod/a
#volume computation
v_s = math.pi*0.25*pow(bore,2)*stroke
v_c= v_s/(r-1)
s_c=math.radians(start_cra) #starting angle of the cranck
e_c=math.radians(end_cra) #ending postion or angle of cranck during compression or expansion
numvalues= 52 #number of values between start and end of cranck in either compression or expansion process
dteta=(e_c-s_c)/(numvalues-1) #change in angle
V=[] #created an empty array to save volume values
for i in range(0,numvalues):
teta=s_c+i*dteta
term1 = 0.5*(r-1)
term2 = R+1-math.cos(teta)
term= pow(R,2)-pow(math.sin(teta),2)
term3=pow(term,0.5)
V.append((1+term1*(term2-term3))*v_c) #append is used to save the values generated in each ittiration in an array
#after calling a function to produce values outside the function it has to commanded to return values
return V
#state 1
v1=v_c+v_s
#state 2
v2=v_c
p2=p1*pow(v1,gama)/pow(v2,gama)
m=(p1*v1)/t1
"""
we know p1 v1 /t1 == p2*v2/t2=p3*v3/t3 cause mass and universal gas contants are always constant on a close cycle.
therfore the ideal gas equation is always constant. let the constant be 'm'
"""
t2=p2*v2/m
v_comp=engine_kinematics(bore,stroke, conn_rod, r, 180,0)
p_compression=[]
for v in v_comp:
p_compression.append(p1*pow(v1,gama)/pow(v,gama))
#state 3
#2-3 constant volume heat addition
v3=v2
p3=m*t3/v3
v_exp=engine_kinematics(bore,stroke, conn_rod, r, 0, 180) #Calling function
p_expansion=[]
for v in v_exp:
p_expansion.append(p3*pow(v3,gama)/pow(v,gama))
"""these volume and pressure arrays used to plot the curve in function of cylinder geometry and compression ratios
"""
#state 4
v4=v1
p4=p3*pow(v3,gama)/pow(v4,gama)
t4=p4*v4/m
plt.figure()
plt.plot([v2,v3],[p2,p3])
plt.plot(v_comp,p_compression)
plt.plot(v_exp,p_expansion)
plt.plot([v4,v1],[p4,p1])
plt.show()
plt.savefig('otto_cycle.png') #to save the plot
#thermal efficiency of otto cycle is given as
#efficiency= 1-(1/r^gamma-1)
n=(1-(1/(pow(r,(gama-1)))))*100
print('efficiency of engine =',n)