Challenge

Week 2 Air standard Cycle

         Write a program in Python to solve an otto cycle and make plots for it


	Objectives
1.	To plot the P-V diagram for the Otto cycle with formulas.
2.	To calculate the thermal efficiency of the cycle

An Otto cycle is an idealized thermodynamic cycle that describes the functioning of a typical spark ignition piston engine. It is the thermodynamic cycle most commonly found in automobile engines cycle of a four-stroke, Spark Ignition(SI), naturally aspirated engine.
 This is the cycle of most automobile engines and other four-stroke SI engines work.

IDEAL GAS LAW = 
It is the general gas equation also called the general gas equation. It is a good approximation for many gases under various conditions.
It is derived as follow:

P V = n R T
P = Pressure, V = Volume, T = Tempertaure, R = Gas Constatnt 



-------------------------------------------------------------------------------

Inputs And Formulas

#Engine Geometric Parameters
Bore = 0.1
Stroke = 0.1
Connecting Rod = Cond_Rod = 0.15
Compression Ratio = cr = 12
Gamma = 1.4
#State Variables
Presssure(p1) = 101325 MPa
Temperature(T1) = 500K
Temperature(T3) = 2300 K
Swept Volume = v_s
Clearance Volume = v_c



NOTE: For simplicity of the program we have considered some state variables like Pressure, Temperature.

-------------------------------------------------------------------------------------



#Otto cycle simulator

import math                          %Importing the libraries
import matplotlib.pyplot as plt

def final_egineKine(bore,stroke, con_rod,cr, start_crank,end_crank):

%Creating a function with command 'def'. This function includes different input parameters



	#geometric parameters
	a = stroke/2               %Calculating the crank pin radius
	R = con_rod/a              %R =Ratio of connecting rod and crank pin radius


	#Volume parmaeters
	v_s = math.pi*(1/4)*pow(bore,2)*stroke  %Swept Volume is volume swept or 
                                                %covered by a piston from its Top dead 
                                                %center(TDC) to Bottom dead center(BDC)
 
	v_c = v_s/(cr-1)                        %Clearance Volume(v_c) is the the 
                                                %clearance or gap between Piston head 
                                                %and TDC positon of the cyclinder head

	sc = math.radians(start_crank)          
	ec = math.radians(end_crank)

	num_values = 50                          %Total number of values to be executed               
	dtheta = (ec - sc)/(num_values-1)

	v = []                                  %Generating the empty array 'V'

	for i in range(0,num_values):
		theta = sc + i*dtheta           %Braking the formula for easy 
                                                %calculation
		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


#STATE POINT 1

##INPUTS

p1 = 101325
t1 = 500
gamma = 1.4
t3 = 2300

#geometric parameters
bore = 0.1
stroke = 0.1
con_rod = 0.15
cr = 12


#Volume computation
v_s = (math.pi/4)*pow(bore,2)*stroke   %caluclating the swept volume
v_c = v_s/(cr-1)
v1 = v_c + v_s


#STATE POINT 2

v2 = v_c

#p2v2^gamma = p1v1^gamma
p2 = p1*pow(v1,gamma)/pow(v2,gamma)

#p2v2/t2 = p1v1/t1| rhs = p1v1/t1| p2v2/t2 = rhs| t2 = p2v2/rhs
rhs = p1*v1/t1
t2 = p2*v2/rhs
v_compression = final_egineKine(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))


#STATE POINT 3

v3 = v2                                %Heat addition at constant volume at V3,V2

#p3v3/t3 = p2v2/t2| rhs = p2v2/t2 | p3 = rhs*t3/v3
rhs= p2*v2/t2
p3 = rhs*t3/v3

v_expansion = final_egineKine(bore,stroke,con_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                   %For expansion stroke we have heat rejection at const volume

#p4v4^gamma = p3v3^gamma

p4 = p3*pow(v3,gamma)/pow(v4,gamma)


#p4v4/t4 = rhs
t4 = p4*v4/rhs


#Thermal efficiency of the engine   
          
%We have calculated the thermal efficiency of the engine with the basic formula available 

eff1 = 1-(t4 -t1)/(t3 - t2)
Efficiency = eff1*100
print(Efficiency)

plt.plot([v2,v3],[p2,p3])                   %Plotting the quantites with plt commoand
plt.plot(v_compression, P_compression)
plt.plot(v_expansion, P_expansion)
plt.plot([v4,v1],[p4,p1])
plt.xlabel('Pressure(P)')                   %Labelling the plots
plt.ylabel('Volume(V)')
plt.title('Figure1:P-V Diagram')            
plt.show()




---------------------------------------------------------------------------------------@@@@CONCLUSION

We have successfully demonstrated the OTTO Cycle with basic formulas and commands.
One could visualize state variables for different crank angles and engine parameters.
We have also calculated the thermal efficiency of the cycle that counts to 62%.
If we go by logic, this efficiency is impossible to achieve practically but for learning code and plotting cycle we can consider it. This was a simple demonstration of code for the basic otto cycle but in the future, we could visualize and plot for different engine types, configurations like V8, W6,v16.
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.