"Based on Example 4.2 in Heywood.  Done with EES and Flair!"

"A spark ignition engine with a compression ratio of 9.5 takes in stoichiometric fuel vapor mixture
 at the start of compression.  Temperature is 400K, p = 1 atm.  Find the temperature
 and pressure at the end of the stroke.  Calculate work done per mass mixture."

r_c = 9.5
T_1 = 400							"[K]"
P_1 = Po#							"[kPa]"

"Let's do the stoichiometry of this mixture"

phi = 1
n_fuel = 1
n_O2 = 12.5/phi
n_N2 = 3.773*n_O2
n_air = n_O2+n_N2
n_u = n_fuel + n_air

y_fuel = n_fuel/n_u
y_O2 = n_O2/n_u
y_N2 = n_N2/n_u
M_u = y_fuel*MOLARMASS(C8H18)+y_O2*MOLARMASS(O2)+y_N2*MOLARMASS(N2)
R_u = R# / M_u

"Next, let's get the properties of this mixture at the start of the compression stroke"

P_1*v_1 = R_u*T_1							"Ideal Gas Law"
u_1 = y_fuel*INTENERGY(C8H18,T=T_1)+y_O2*INTENERGY(O2,T=T_1)+y_N2*INTENERGY(N2,T=T_1)
s_1 = y_fuel*ENTROPY(C8H18,T=T_1,P=y_fuel*P_1)+y_O2*ENTROPY(O2,T=T_1,P=y_O2*P_1)+y_N2*ENTROPY(N2,T=T_1,P=y_N2*P_1)

"Next, the properties at the end of the compression stroke"

P_2*v_2 = R_u*T_2							"Ideal Gas Law"
v_2 = v_1/r_c								"Volume change with compression"
u_2 = y_fuel*INTENERGY(C8H18,T=T_2)+y_O2*INTENERGY(O2,T=T_2)+y_N2*INTENERGY(N2,T=T_2)
s_2 = y_fuel*ENTROPY(C8H18,T=T_2,P=y_fuel*P_2)+y_O2*ENTROPY(O2,T=T_2,P=y_O2*P_2)+y_N2*ENTROPY(N2,T=T_2,P=y_N2*P_2)

"Isentropic Process"

s_2 = s_1

"Calculate the work with the first law of thermodynamics in kJ/(kg air)"

w_12 = (u_2 - u_1)*(n_u/(n_air*MOLARMASS(Air)))