Challenge

Week 2 Air standard Cycle

import math
import matplotlib.pyplot as plt


def engine_kinematics(bore,stroke,c_rod,cr,start_crank,end_crank):

	a=stroke/2
	R=c_rod/a

	V=[]
	num_values=50
	sc=math.radians(start_crank)
	ec=math.radians(end_crank)
	dtheta= (ec-sc)/(num_values-1)

	for i in range (0, num_values):
		theta =sc + i*dtheta
		term1=pow(R,2)-pow(math.sin(theta), 2)
		term2=pow(term1, 0.5)
		term3=R+1-math.cos(theta)
		v= (1+ 0.5*(cr-1)*(term3-term2))*vc
		V.append(v)
	return(V)


# calculating state variables

#Geometri parameters
bore= 0.1
stroke= 0.1
c_rod= 0.15
cr=10

#initial variables
p1=101325
t1=500
gamma=1.4
p1=101325
t3=2300

vs=math.pi*(1/4)*pow(bore,2)*stroke

#clearance volume
vc= vs/(cr-1)

#Total volume
v1= vs+vc
v2=vc

#state point 2 : process (1-2) is the adiabatic compression (p*pow(v,gamma))
# p2*pow(v2,gamma)=p1*pow(v1,gamma)=rhs
rhs= p1*pow(v1,gamma) 
p2=rhs/pow(v2,gamma)

v_compression = engine_kinematics(bore,stroke,c_rod,cr,180,0)

p_compression= []

for v in v_compression:
	p_compression.append(rhs/pow(v,gamma))


# from ideal gas law equation (pv=nrt): p1*v1/t1=p2*v2/t2
# p2*v2/t2 = p1*v1/t1 = rhs
rhs=p1*v1/t1
t2= p2*v2/rhs

#state point 3 : process (2-3) follows constant volume heat addition
v3=v2

# p3*v3/t3 = p2*v2/t2 = rhs : from ideal gas law
p3 = rhs*t3/v3

v_expansion = engine_kinematics(bore,stroke,c_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 : process (4-1) follows constant volume heat rejection
v4=v1

# p4*pow(v4,gamma) = p3*pow(v3,gamma): since process (3-4) follows adiabatic expansion
rhs = p3*pow(v3,gamma)
p4 = rhs/pow(v4,gamma)

# p4*v4/t4 = p3*v3/t3 = rhs : ideal gas law
rhs = p3*v3/t3
t4 = p4*v4/rhs

#Calculating thermal effeciency (n)

term5= (t4-t1)/(t3-t2)
n= 1- term5
print('thermal effeciency =', n)


plt.plot([v2,v3],[p2,p3])
plt.plot([v4,v1],[p4,p1])
plt.plot(v_compression,p_compression)
plt.plot(v_expansion,p_expansion)
plt.show()

Code explanation:

  1. Importing math and matplotlib librabies
  2. Defining the function to calculate the volumes during compression and expansion (using def_engine kinematics(inputs))
    1. The function takes the inputs and returns the array

Calculating the state variables

  1. Assigning the Geometric parameters and initial variables
  2. Calculating the swept, clearance and total volume
  3. At state point 2,
    1. using the process "adiabatic compression", calculating the pressure (p2).
    2. Since the compression starts at state point 2, calculating the pressure_compression array using volume_compression array which was found by calling the function (def_enginekinematics(inputs))
    3. Calcuating the temperature (t2) using ideal gas equation
  4. At state point 3,
    1. v2=v3
    2. Calculating the pressure using the ideal gas equation
    3. Since the expansion starts at point 3, calcuating the pressure_expansion array using volume_expansion array which was found by calling the function (def_enginekinematics(inputs))
  5. At state point 4,
    1. v4=v1
    2. calculating the p4 using adiabatic expansion and t4 using ideal gas equation
  6. Calcuating the thermal effeciency using the formula
  7. Then plotting the curve using the commands
    1. plt.plot([v2,v3],[p2,p3])
      plt.plot([v4,v1],[p4,p1])
      plt.plot(v_compression,p_compression)
      plt.plot(v_expansion,p_expansion)
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.