AIR STANDARD CYCLE
AIM: To write a python program to simulate the otto cycle.
OBJECTIVE: 1.To write a program to plot the graph between pressure and volume.
2.To calculate the thermal efficiency for given compression ratio.
THEORY:
The Otto cycle is the theoretical cycle of interest when one is considering reciprocating SI engines. The four-stroke Otto cycle is made up of the following four internally reversible processes:
1–2 - Isentropic compression.
2–3 - constant-volume heat addition.
3–4 - Isentropic expansion.
4–1 - constant-volume heat rejection.

Thermodynamic analysis of the Otto cycle produces a simple relationship among the thermal efficiency of the Otto cycle ηth, Otto, compression ratio r, and specific-heat ratio k

Process 1-2: Isentropic compression:
In this process, the piston moves from bottom dead centre (BDC) to top dead centre (TDC) position. Air undergoes reversible adiabatic (isentropic) compression. We know that compression is a process in which volume decreases and pressure increases. Hence, in this process, volume of air decreases from V1 to V2 and pressure increases from p1 to p2. Temperature increases from T1 to T2. As this an isentropic process, entropy remains constant (i.e., s1=s2)
Process 2-3: Constant Volume Heat Addition:
Process 2-3 is isochoric (constant volume) heat addition process. Here, piston remains at top dead centre for a moment. Heat is added at constant volume (V2 = V3) from an external heat source. Temperature increases from T2 to T3, pressure increases from p2 to p3 and entropy increases from s2 to s3

Process 3-4: Isentropic expansion:
In this process, air undergoes isentropic (reversible adiabatic) expansion. The piston is pushed from top dead centre (TDC) to bottom dead centre (BDC) position. Here, pressure decreases fro p3 to p4, volume rises from v3 to v4, temperature falls from T3 to T4 and entropy remains constant (s3=s4). (Refer p-V and T-s diagrams above).
Process 4-1: Constant Volume Heat Rejection:
The piston rests at BDC for a moment and heat is rejected at constant volume (V4=V1). In this process, pressure falls from p4 to p1, temperature decreases from T4 to T1 and entropy falls from s4 to s1.

PROCEDURE:
1.Import the matplotlib.pyplot library.
2.Define the function engine kinematics
3.Calculate the crank pin radius,swept volume and clearance volume.
4.Then for loop is initiated to calculate the volume for different theta values using following formula.
V = 1 + 1/2(C_R - 1)[R +1 - COS(theta) - (R^2 - SIN^2(theta))^1/2]
5.Give all the required inputs such as bore,stroke length,connecting rod length,compression ratio.
6.Then plot the graph bewteen pressure and volume.
FORMULAS USED:
1.PV^γ = constant.
2.PV=nRT
3.PV = constant
4.V = 1 + 1/2(C_R - 1)[R +1 - COS(theta) - (R^2 - SIN^2(theta))^1/2]
5. 
PROGRAM CODE:
#OTTO CYCLE SIMULATOR
import math
import matplotlib.pyplot as plt
#function defination
def engine_kinematics(bore,stroke,con_rod,cr,start_crank,end_crank):
a = stroke/2 #crank pin radius
R = con_rod/a #constant
print("crank pin radius=",a)
print("constant R=",R)
#VOLUME COMPUTATION
v_s = (math.pi/4)*pow(bore,2) #swept volume
v_c = v_s/(cr-1) #clearance volume
print("values of swept volume v_s=",v_s)
print("values of clearance volume v_c=",v_c)
sc = math.radians(start_crank)
ec = math.radians(end_crank)
num_values = 50
dtheta = (ec-sc)/(num_values-1)
V=[]
for i in range(0,num_values):
theta = sc+i*dtheta
term1 = 0.5*(cr - 1)
term2 = R+1-math.cos(theta)
term3 = pow(R,2)-pow(math.sin(theta),2)
term3 = pow(term3,0.5)
V.append((1+term1*(term2-term3))*v_c)
print("VOLUME=",V)
return V
#inputs
p1 = 101325 #pressure
t1 = 500 #temperature
gamma = 1.4 #gas constant
t3 = 2300
#GEOMETRIC PARAMETERS
bore = 0.1
stroke = 0.1
con_rod = 0.15
cr = 8
#VOLUME PARAMETERS
V_s = (math.pi/4)*pow(bore,2)
V_c = V_s/(cr-1)
v1 = V_s + V_c
print("v1=",v1)
#state point 2
v2 = V_c
print("v2=",v2)
#PV^gamma = constant
p2 = p1*pow(v1,gamma)/pow(v2,gamma)
#PV/T = constant
k = p1*v1/t1
t2 = p2*v2/k
print("p2=",p2)
print("t2=",t2)
V_compression = engine_kinematics(bore,stroke,con_rod,cr,180,0)
constant = p1*pow(v1,gamma)
P_compression = []
for V in V_compression:
P_compression.append(constant/pow(V,gamma))
print("P_compression=",P_compression)
#state point 3
v3 = v2
print("v3=",v3)
#PV/T = constant
k = p2*v2/t2
p3 = k*t3/v3
print("p3=",p3)
V_expansion = engine_kinematics(bore,stroke,con_rod,cr,180,0)
constant = p3*pow(v3,gamma)
P_expansion = []
for V in V_expansion:
P_expansion.append(constant/pow(V,gamma))
print("P_expansion=",P_expansion)
#state point 4
v4 = v1
print("v4=",v4)
# PV^gamma = constant
p4 = p3*pow(v3,gamma)/pow(v4,gamma)
print("p4=",p4)
#PV/T = constant
t4 = p4*v4/k
print("t4=",t4)
#THERMAL EFFICIENCY
f1 = t4-t1
f2 = t3-t2
N = (1-(f1)/(f2))*100
print("thermal efficiency =",N)
#plotting the graph between pressure and volume
ax=plt.axes()
ax.set_facecolor('lightgrey')
plt.grid(True,color='black',linestyle='dotted')
plt.plot([v2,v3],[p2,p3],color='red',linewidth=1.5)
plt.plot(V_compression,P_compression,color='green',linewidth=1.5)
plt.plot(V_expansion,P_expansion,color='purple',linewidth=1.5)
plt.plot([v4,v1],[p4,p1],color='blue',linewidth=1.5)
plt.xlabel("VOLUME",color='green',fontsize='large',fontweight='bold')
plt.ylabel("PRESSURE",color='purple',fontsize='large',fontweight='bold')
plt.title("OTTO CYCLE",color='black',fontsize='x-large',fontweight='bold')
plt.show()
OUTPUT:
v1= 0.008975979010256553
v2= 0.001121997376282069
v3= 0.001121997376282069
v4= 0.008975979010256553
p2= 1862269.7731211928
p3= 3728760.0000000005
p4= 202879.63239975358
t2= 1148.6983549970346
t4= 1001.133147790543
crank pin radius= 0.05
constant R= 2.9999999999999996
values of swept volume v_s= 0.007853981633974483
values of clearance volume v_c= 0.001121997376282069
VOLUME = [0.008975979010256555, 0.008970597604577385, 0.008954446066984628, 0.008927502750001736, 0.008889732625790873, 0.008841088876673947, 0.008781515147633564, 0.008710948483263278, 0.008629322973270154, 0.008536574128585385, 0.008432644003555496, 0.008317487067775301, 0.008191076813241317, 0.00805341305827189, 0.00790452987908024, 0.007744504063567808, 0.0075734639410790896, 0.007391598398549511, 0.007199165850499001, 0.006996502891184618, 0.006784032325902513, 0.006562270258998323, 0.006331831912301289, 0.006093435862211276, 0.005847906417874087, 0.0055961739162863995, 0.00533927278029369, 0.005078337267924873, 0.00481459493053058, 0.004549357886143298, 0.004284012096743069, 0.004020004907876926, 0.003758831161988034, 0.0035020182303793627, 0.0032511103224774646, 0.0030076524263113556, 0.0027731742136548167, 0.00254917421079626, 0.002337104495480024, 0.0021383561361875917, 0.001954245545112389, 0.0017860018737466048, 0.0016347555419748924, 0.001501527959214727, 0.0013872224700342236, 0.00129261653684447, 0.0012183551583344345, 0.0011649455136575537, 0.00113275281820679, 0.001121997376282069]
P_compression = [101324.99999999997, 101410.10814045893, 101666.28565964484, 102096.10699081204, 102703.91371881544, 103495.89227773559, 104480.18558971255, 105667.04163888533, 107069.00302558525, 108701.14275790153, 110581.35296230171, 112730.6948948669, 115173.82068646484, 117939.4797527374, 121061.12585838727, 124577.6445860576, 128534.2255949467, 132983.4097704967, 137986.3484106609, 143614.32025359303, 149950.56274956622, 157092.48686067213, 165154.3601635551, 174270.56137041806, 184599.53057524515, 196328.5630841138, 209679.61915033433, 224916.3440919968, 242352.50676568714, 262362.05739976803, 285390.95729015546, 311970.8063712094, 342734.02873548435, 378429.86997347133, 419939.5536532184, 468287.39414499316, 524642.1239655045, 590298.7307426519, 666625.2951700741, 754951.6175947096, 856367.9184432976, 971396.4183308952, 1099505.0449841206, 1238466.830069439, 1383651.159404096, 1527473.1373487064, 1659387.0754888728, 1766864.7793821557, 1837561.826338372, 1862269.7731211928]
P_expansion = [202879.63239975352, 203050.04156087406, 203562.97717322558, 204423.59393774346, 205640.5848634521, 207226.3368388008, 209197.15416233815, 211573.55602730333, 214380.6560595987, 217648.6344352943, 221413.31595617242, 225716.8708654268, 230608.65929380822, 236146.24522727405, 242396.61201134874, 249437.61893754272, 257359.7477373776, 266268.1992549141, 276285.41467297514, 287554.1130065575, 300240.9577968732, 314540.9917301384, 330683.008923746, 348936.0713439833, 369617.41825090355, 393102.06472318614, 419834.43429499294, 450342.4151007234, 485254.2559465225, 525318.704770866, 571428.6948456996, 624648.6308023258, 686244.7940857798, 757717.3738890146, 840830.8144612043, 937636.0659420155, 1050473.2420582036, 1181935.2528903068, 1334761.3603008324, 1511614.178704308, 1714676.6197159474, 1944994.308072137, 2201501.893393071, 2479740.3920431864, 2770438.0813165144, 3058407.980318814, 3322534.812574238, 3537733.796606202, 3679288.110923747, 3728760.0000000005]
Thermal efficiency = 56.47247183519378
GRAPH:

CONCLUSION: The simulation of air standard cycle/otto cycle has been done using python.