Week 2 Air standard Cycle
#Program to run Otto cycle and plot P-V diagram and find thermal efficiency of engine
import math
import matplotlib.pyplot as plt
#defining a function "engine_kinematics" consisting all necessary parameters
def engine_kinematics(bore, stroke, conn_rod, cr, start_crank, end_crank):
#geometric parameters of engine
a = stroke/2
R = conn_rod/a
#volume parameters of engine
swept_v = math.pi*(1/4)*pow(bore,2)*stroke
clearance_v = swept_v/(cr-1)
sc = math.radians(start_crank)
ec = math.radians(end_crank)
num_values = 20
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))*clearance_v)
return V
#geometric parameters in meter
bore = 0.15
stroke = 0.15
conn_rod = 0.2
#compression ratio between (cr) 8:1 - 12:1, specific heat (gamma)
cr = 8
gamma = 1.4
#inputs for state 1 pressure (Pa), temperature (K)
p1 = 111325
t1 = 700
t3 = 2500
#volume computation as per Otto cycle formulas
swept_v = math.pi*(1/4)*pow(bore,2)*stroke
clearance_v = swept_v/(cr-1)
v1 = clearance_v + swept_v
#state point 2
v2 = clearance_v
#p2v2^gamma = p1v1^gamma
p2 = p1*pow(v1,gamma)/pow(v2,gamma)
#p2v2/t2 = p1v1/t1 :: t2 = (p2v2)t1/(p1v1)
t2 = ((p2*v2)*t1)/(p1*v1)
v_compression = engine_kinematics(bore, stroke, conn_rod, cr, 180, 0)
constant = p1*pow(v1,gamma)
p_compression = []
for v in v_compression:
p_compression.append(constant/pow(v,gamma))
#state point 3
v3 = v2
#p3v3/t3 = p2v2/t2 :: p3 = p2v2t3/t2v3
p3 = (p2*v2*t3)/(t2*v3)
v_expansion = engine_kinematics(bore, stroke, conn_rod, cr, 0, 180)
constant = p3*pow(v3,gamma)
p_expansion = []
for v in v_expansion:
p_expansion.append(constant/pow(v,gamma))
#state point 4
v4 = v1
#p4v4^gamma = p3v3^gamma
p4 = p3*pow(v3,gamma)/pow(v4,gamma)
#p4v4/t4 = p3v3/t3
t4 = (p4*v4)*t3/(p3*v3)
#thermal efficiency of engine = 1 - 1/cr^(gamma - 1)
thermal_efficiency = (1 - 1/(pow(cr,(gamma-1))))*100
print("thermal efficiency = ",thermal_efficiency,"%")
plt.plot([v2,v3],[p2,p3])
plt.plot(v_compression,p_compression)
plt.plot(v_expansion,p_expansion)
plt.plot([v4,v1],[p4,p1])
plt.xlabel("Volume(m^3)")
plt.ylabel("pressure(Pa)")
plt.show

Explanation:
Starting with comment to state the purpose of code.
Then defining a function " def engine_kinematics(bore, stroke, conn_rod, cr, start_crank, end_crank):" consisting of geometric parameters of engine.
Under function formulas are written to calculate clearance volume and swept volume further calculating volume at various position of piston defined by number of crank angle "num_values" from "start_crank" to "end_crank", here "num_values" chosen "20" higher the number finer the values/results.
All calculated values of volume are appended in "V=[]".
Value of compression ratio (cr = 8) and specific heat ratio (gamma = 1.4) been selected.
Now for state 1 values of temperature, pressure been considered, depending on these values further temperatures, pressures and volumes been calculated as per Otto cycle formulas/relations between each state of engines (1 to 4).
As volume changes non-linearly in cylinder during compression stoke and expansion stroke same has been calculated using v_compression and v_expansion with help of p_compression, p_expansion and constant.
After calculating all parameter at all states of cycle theoretical thermal efficiency of Otto cycle been calculated.
Lastly PV diagram has been plotted with "xlabel" for volume and "ylabel" for pressure axis.