Week 2 Air standard Cycle
AIM: To write a code in python to solve an air standard cycle (OTTO CYCLE).
OBJECTIVE:
- Calculate the state variables and plot P-V diagram for it.
- Calculate the thermal efficiency of the engine.
GOVERNING EQUATIONS:
Adiabatic process- (PV)^γ=constant.
Ideal Gas Law- PV/T= constant.
Piston Volume-

Thermal efficiency- 1-(1/cr)^gamma-1
Swept volume- π/4*(bore)^2*stroke
Clearence volume= swept volume/(cr-1)
where cr is the compression ratio.
THEORY:
An otto cycle is an idealized thermodynamic cycle that describes the functioning of a typical I.C engine. An otto cycleis a description of what happens to a mass of gas as it is subjected to changes of pressure,volume,addition of heat,and removal of heat. The mass of gas subjected to those changes is called the system.
The otto cycle is constructed from:
Top and bottom of the loop , a pair of isentropic processes and,
Left and right sides of loop, a pair of parallel isochoric processes(constant volume).
- Process 0–1 a mass of air is drawn into piston/cylinder arrangement at constant pressure.
- Process 1–2 is an adiabatic (isentropic) compression of the charge as the piston moves from bottom dead center to top dead center.
- Process 2–3 is a constant-volume heat transfer to the working gas from an external source while the piston is at top dead center. This process is intended to represent the ignition of the fuel-air mixture and the subsequent rapid burning.
- Process 3–4 is an adiabatic (isentropic) expansion (power stroke).
- Process 4–1 completes the cycle by a constant-volume process in which heat is rejected from the air while the piston is at bottom dead center.
- Process 1–0 the mass of air is released to the atmosphere in a constant pressure process.

PROCEDURE:
In starting we will add our basic libraries/modules into the sublime which are essential for the calculation of mathematical terms and also used for the plotting of the graph which is required for this which are "math" and "matplotlib".
After that we are defining the function named as " engine_kinematics" which will calculate the volume of the piston and is also helpful in generating the right type of the plot. While coding for the function as we know that the equation is too big so we have divided the equation in three terms for the ease of the calulation. In this we have efined the values of theta to be 100 so that we can get 100 results for it and all these values are stored in an array named V.
After defining the function we are defining the input parameters for all the 4 states. Some input variables are defined here and else we will calculate. After that the engine geometric parameters are defined like bore,stroke,cr etc. and we will calculate our swept and the clearence volume.
We will give our maximum temperature during the process which is 2500k which is known and by using the ideal gas law and the adiabatic process equation we will find our rest state variables.
If we plot the PV diagram after calculating all these variables we can see that the graph is not accurate as the volume can be seen is not perfect so to have the right plot we will call our function in betwwen our compression and expansion proscess so that we can have our piston volume and in accordance with we can calculate our pressure for the the compression and the expansion.
For the pressure values we will first define an empty arrays for both the cases so that after calling the function we can store the values in it by appending.
This process is done for both the compression stroke and the expansion stroke so that we can have our volume and pressure traces perfectly. The loop will follow for 100 values as we have given our "num" value 100 and we will get the more smooth curve.
In last we will plot our these points and give them labels and we can see the plot for our PV diagram.
Finally we calculate the thermal efficiency and print it out in our output window.
CODE:
# A program for Air Standard Cycle (OTTO CYCLE)
import math
import matplotlib.pyplot as plt
# Defining Function
def engine_kinematics(bore,stroke,con_rod,cr,start_crank,end_crank):
a=stroke/2 #crank pin radius
R=con_rod/a
v_s=(math.pi/4)*pow(bore,2)*stroke #Swept volume
v_c=v_s/(cr-1) #Clearence volume
SC= math.radians(start_crank) #SC- Starting crank angle
EC= math.radians(end_crank) #EC- Ending crank angle
num=100
dtheta=(EC-SC)/(num-1)
#Piston volume
V=[]
# For loop
for i in range(0,num):
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)
return V
# Input Variables
gamma=1.4
p1=101325 #Atmospheric pressure at stage 1
t1=400
t3=2500 # MAx temp
#Geometric parameters
bore=0.12
stroke=0.1
con_rod=0.15
cr=12
# Volumetric Computations
v_s=(math.pi/4)*pow(bore,2)*stroke # Swept volume
v_c=v_s/(cr-1)
v1=v_s+v_c
v2=v_c
# State variable at 2
#p2*v2^gamma=p1*v1^gamma Isentropic Compression
p2=p1*pow(cr,gamma)
t2=p2*v2*t1/(p1*v1)
V_comp= engine_kinematics(bore,stroke,con_rod,cr,180,0)
C=p1*pow(v1,gamma)
P_comp=[]
for v in V_comp:
P_comp.append(C/pow(v,gamma))
# State variable at 3
v3=v2 #Isochoric process
p3=p2*t3/t2
V_exp= engine_kinematics(bore,stroke,con_rod,cr,0,180)
C=p3*pow(v3,gamma)
P_exp=[]
for v in V_exp:
P_exp.append(C/pow(v,gamma))
# State variables at 4
v4=v1
p4=p3*pow(v3,gamma)/pow(v4,gamma)
t4=p4*v4*t3/(p3*v3)
#Plotting 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.xlabel('Volume(m^3)')
plt.ylabel('Pressure(Pa)')
plt.title('PV diagram for OTTO CYCLE')
plt.grid()
plt.show()
#Calculating Thermal efficiency
Efficiency= (1-(1/pow((cr),(gamma-1))))*100
print('Efficiency is=',Efficiency)
OUTPUT/RESULTS:
The PV diagram which we formed during this coding is as follows:

The thermal efficiency for the engine is:

CONCLUSION:
Hence the PV diagram and the thermal efficiency for this air standard cycle is plotted successfully and the efficiency is calculated which is 62.9%.
ERRORS:
Just some common mistakes were made while coding like the spelling mistakes which were corrected after getting the error.