Here it is. I am new at this, so it is very possible that I am doing something (multiple things) very wrong.
--# Main
-- Ottomatic
supportedOrientations(LANDSCAPE_ANY)
-- Use this function to perform your initial setup
function setup()
p={}
V={}
--V[2]=25
T={}
Wk={net=0,net_engine=0}
Q={add=0,rej=0,add_engine=0,rej_engine=0}
DeltaU={}
DeltaS={}
HeatRate={add=0,rej=0}
--CV=0
--r_v=0
RPM=3000
Engine_RPM=tostring(RPM)
HeatAdd=20000
Heat_Added=tostring(HeatAdd)
AF=15
AF_Ratio=tostring(AF)
t=os.date("Today is: %B %d, %Y")
parameter.integer("Cylinders", 1, 12, 2 )
parameter.boolean("Two_Stroke", true)
parameter.integer("Bore_mm",1.0,200,100)
parameter.integer("Stroke_mm",1,200,100)
parameter.integer("Ratio_Select",1,2,1)
parameter.integer("Compression Ratio",10,150,90)
parameter.text("Clearance_Volume_cc","")
parameter.integer("T1_degC",0,100,25)
parameter.integer("p1_kPa",100,300,100)
parameter.text("Engine_RPM",RPM)
parameter.integer("Heat_Select",1,3,1)
parameter.text("Heat_Added",HeatAdd)
parameter.text("AF_Ratio",AF)
k=1.399
R=0.287
c_v = R/(k-1)
end
function EngineCalcs()
-- convert input values
Bore = Bore_mm/10.0
Stroke = Stroke_mm/10.0
V_D = math.pi/4*Bore^2*Stroke
p[1] = p1_kPa
T[1] = T1_degC+273.15
if Two_Stroke then StrokeNum=2 else StrokeNum=4 end
-- select compression ratio or clearnance volume and calculate V1
if Clearance_Volume_cc ~= "" then CV=tonumber(Clearance_Volume_cc) else CV = 0.1 end
if Ratio_Select==1
then
r_v = Compression_Ratio/10.0
V[2] = V_D/(r_v-1)
V[1] = V[2] + V_D
Clearance_Volume_cc = tostring(V[2])
else
V[2] = CV
V[1] = V[2] + V_D
r_v = V[1]/V[2]
end
-- calculate other volume values
V[3] = V[2]
V[4] = V[1]
V_D_Engine = V_D*Cylinders
-- thermal efficincy and engine speed for power calcs
Thermal_Eff = 1-(1/r_v^(k-1))
RPM = tonumber(Engine_RPM)
if RPM == "" then Speed=0 else Speed = RPM end
if Two_Stroke then N=Speed/60 else N=Speed/(2*60) end
-- mass of air/fuel mixture
m = p[1]*(V[1]/100)/(R*T[1])
-- calculate p2 and T2
p[2] = p[1]*r_v^k
T[2] = p[2]*(V[2]/100^3)/(m*R)
-- calculate heat added, T3, and p3
if AF_Ratio=="" then AF=0 else AF=tonumber(AF_Ratio) end
if Heat_Added=="" then HeatAdd=0 else HeatAdd=tonumber(Heat_Added) end
if Heat_Select==1
then Q.add = HeatAdd*m/(AF+1)
elseif Heat_Select==2
then Q.add = HeatAdd*m
else
Q.add = HeatAdd
end
T[3] = T[2]+Q.add/(m*c_v)
p[3] = p[2]*T[3]/T[2]
-- calculate p4 and T4
p[4] = p[3]*r_v^-k
T[4] = p[3]*(V[3]/100^3)/(m*R)
-- calculate work from 1-2 and 3-4
Wk[12] = 1/(1-k)*(p[2]*V[2]-p[1]*T[1])
Wk[34] = 1/(1-k)*(p[4]*V[4]-p[3]*T[3])
-- internal energy changes
DeltaU[12] = -Wk[12]
DeltaU[23] = Q.add
DeltaU[34] = -Wk[34]
DeltaU[41] = Q.rej
-- entropy changes
DeltaS[23] = m*c_v*math.log(T[3]/T[2])
DeltaS[41] = m*c_v*math.log(T[1]/T[4])
-- net work, net heat, power, and heat rate
Wk.net = Wk[12]+Wk[34]
Wk.net_engine = Wk.net*Cylinders
Power = Wk.net_engine*N
Q.net = Q.add+Q.rej
Q.add_engine = Q.add*Cylinders
Q.rej_engine = Q.rej*Cylinders
HeatRate.add = Q.add_engine*N
HeatRate.rej = Q.rej_engine*N
end
-- This function gets called once every frame
function draw()
EngineCalcs()
-- This sets a dark background color
background(210, 210, 215, 255)
-- This sets the line thickness
strokeWidth(4)
-- Do your drawing here
stroke(0)
line(0,HEIGHT-150,WIDTH,HEIGHT-150)
displayMode(STANDARD)
textMode(CENTER)
fill(0)
font("Courier-Bold")
textWrapWidth(800)
fontSize(24)
text("The",WIDTH/2,HEIGHT-25)
fontSize(100)
text("OTTO",WIDTH/2.5,HEIGHT-75)
fontSize(32)
text("...matic",2*WIDTH/3,HEIGHT-120)
textMode(CORNER)
fontSize(12)
text(tostring(t),20,HEIGHT-148)
fontSize(24)
text("Engine Specifications:",10,HEIGHT-190)
fontSize(14)
font("HelveticaNeue")
text("Adjust sliders to change engine values.",400,HEIGHT-175)
text("Hide keyboard after typing vales.",400,HEIGHT-190)
font("Courier")
fontSize(16)
text("Bore: "..string.format("%4.1f",Bore).."cm",40,HEIGHT-220)
text("Stroke: "..string.format("%4.1f",Stroke).."cm",180,HEIGHT-220)
text(Cylinders.."-Cylinder",360,HEIGHT-220)
text(StrokeNum.."-Stroke",500,HEIGHT-220)
if Ratio_Select==1
then
font("Courier-Oblique")
text("Compression Ratio Option Selected",40,HEIGHT-250)
font("Courier")
text(string.format("%.1f",r_v)..":1 Compression Ratio",60,HEIGHT-280)
text("Clearance Volume: "..string.format("%4.1f",V[2]).."cc",340,HEIGHT-280)
else
font("Courier-Oblique")
text("Clearance Volume Option Selected",40,HEIGHT-250)
font("Courier")
text("Clearance Volume: "..string.format("%4.1f",V[2]).."cc",60,HEIGHT-280)
text(string.format("%4.1f",r_v)..":1 Compression Ratio",340,HEIGHT-280)
end
if Heat_Select==1
then
font("Courier-Oblique")
text("Fuel Heating Value Option Selected",40,HEIGHT-310)
font("Courier")
text("Fuel Heating Value: "..HeatAdd.."kJ/(kg of fuel)",60,HEIGHT-340)
text("Air Fuel Ratio: "..AF..":1",500,HEIGHT-340)
elseif Heat_Select==2 then
font("Courier-Oblique")
text("Specific Heat Added Option Selected",40,HEIGHT-310)
font("Courier")
text("Specific Heat Added: "..HeatAdd.."kJ/kg",60,HEIGHT-340)
else
font("Courier-Oblique")
text("Total Heat Added per Cylinder Option Selected",40,HEIGHT-310)
font("Courier")
text("Total Heat Added per Cylinder: "..HeatAdd.."kJ",60,HEIGHT-340)
end
--text(RPM.." RPM",40,HEIGHT-310)
line(0,HEIGHT-350,WIDTH,HEIGHT-350)
fontSize(24)
font("Courier-Bold")
text("Results:",10,HEIGHT-390)
fontSize(16)
font("Courier")
text("Cylinder Displacement Volume: "..string.format("%.1f cc",V_D),20,HEIGHT-420)
text("Thermal Efficiency: "..string.format("%.1f%%",Thermal_Eff*100),20,HEIGHT-450)
text("Engine Speed: "..N.." cyles/second",20,HEIGHT-480)
end
function keyboard(key)
if CCActiveTextBox then
CCActiveTextBox:acceptKey(key)
end
end
function touched(touch)
-- Codea does not automatically call this method
end