# Railcraft Boiler Calculator in CC

help java lua

3 replies to this topic

### #1Paramecium13

• New Members
• 2 posts

Posted 11 February 2014 - 09:46 PM

I am attempting to create a program that calculates the amount of energy produced by a steam boiler added by the mod Railcraft when it is given a user inputted amount of fuel. It first calculates the amount of steam produced and then converts it into MJ and EU, returning the values for all of the above. There is already a web based calculator, which has been shown to be highly accurate (though I noticed it did not implement the fuel value changes outlined here). My program could eventually be used with OpenPeripherals to detect the types and amount of fuels in a chest and return the amount of energy that can be produced by them. The only problem with the code I came up with is that it does not calculate the correct amount of steam produced.

Here is the code I came up with.
```local running = 1
local fuel = 0
while running == 1 do
print("Charcoal (a);Coal (b ); Coke (c); Block of Coke(d); Block of Coal (e)")
write("Fuel:")
if input == "a" then
mult = 1600
elseif input == "b" then
mult = 1600
elseif input == "c" then
mult = 3200
elseif input == "d" then
mult = 28800
elseif input == "e" then
mult = 16000
elseif input == "stop" then
running = 0
end
if running == 1 then
write("Number:")
fuel = fuel + (number * mult)
end
end
write(fuel)
print(" Heat Units")
print("High pressure (a) or low pressure (b )")
write(":")
local tm = 0
local H = 0.05
local EH = 0.8
local EP = 4
local FPBC = 8
local sigma = 160
local N = 0
if input2 == "a" then
tm = 1000
elseif input2 == "b" then
tm = 500
end
write("Number of boilers:")
local steam = 0
local temp = 20
local n = 1
while fuel > 0 do
if temp < tm then
temp = temp + (H + (((tm - temp) / tm) * H * 3) / N)
elseif temp > tm then
temp = tm
end
fuel = fuel - N * (FPBC + EH * (temp / tm) + EP * (tm / 1000))
if temp > 100 or temp == 100 then
steam = steam + sigma * N * (temp / tm)
end
n = n + 1
end
write(temp)
print(" degrees C.")
write(steam)
print(" mB of steam produced in heat up.")
write(n)
print(" cycles with fuel")
n = 0
local tvar = 1 - ((3 * H) / (tm * N))
while temp > 100 do
temp = tvar * temp - H
steam = steam + sigma * N * (temp / tm)
n = n + 1
end
write(n)
print(" cycles to cool down to bellow 100")
write(steam)
print(" mB of steam produced")
local MJ = steam * 0.2
local EU = steam * 0.625
write(MJ)
print(" MJ")
print("Or")
write(EU)
print(" EU can be produced.")
```
Note: please remove the space after the b's in parenthesis. I had to put them in so this editor would not mess them up.

The first part of my code (from "local running = 1" to "print(" Heat Units")") calculates the amount of heat units (or fuel units) that the inputted fuels can produce. It allows for the user to input different kinds of fuels, which the online calculator does not (though it does allow the user to input a custom fuel, which could be used to the same effect). My program currently offers only a limited number of different fuels, though I plan to add more when it actually works. I also plan to create a program or an option within this program that uses liquid fuels instead.

The second part of my code (from "print("High pressure (a) or low pressure (b ) to "N = read()") allows the user to input what type of boiler they are using (high pressure or low pressure) and how many tanks it has (e.g. a 2x2x2 boiler has 8 tanks). This section also sets up several variables that will be used in the next part of the script. The variable "tm" is the maximum temperature of the boiler (1000 for high pressure and 500 for low pressure). The variables H, EH, EP, FPBC, sigma, and N are the Heat Step, fuel heat inefficiency, fuel pressure inefficiency, fuel per boiler cycle, number of tanks, and steam per unit water.

The third part of my code (from "local steam = 0" to "print(" cycles with fuel")") is where my problems occur (there are also likely problems in the fourth part). The first three lines set up the initial values for variables used within the while loop. The while loop simulates the boiler when it is using fuel. I obtained the formulas it uses from this page, which was written by the author of Railcraft, CovertJaguar. It returns several values (maximum temperature, steam produced while the boiler had fuel, and number of cycles with fuel) that can be used for debugging or improving it.

The fourth part of my code simulates the boiler after it has consumed all of its fuel until it reaches a temperature bellow 100 degrees, at which no more steam will be produced. Then it returns the number of cycles spent cooling down, the total steam produced, and how much energy (EU or MJ) that steam can produce.

Can anyone find what is wrong with what I wrote? The source I used contains source code (in Java) from Railcraft that modifies the fuel and temperature. However, I do not understand Java so it would be helpful if someone who does could compare it to the code I wrote. Additionally, the formulas from the source I used use boiler cycles (the source gives a value for them in ticks) but the calculator seems to run on ticks. I think this may be a source of error. Any help or advice would be greatly appreciated.

### #2Bomb Bloke

Hobbyist Coder

• Moderators
• 7,099 posts
• LocationTasmania (AU)

Posted 12 February 2014 - 02:26 AM

Your "heating up" and "fuel usage" code appears to match that at the top of this document pretty well, but it allows the temperature to exceed the maximum every so often:

```if temp < tm then
temp = temp + (H + (((tm - temp) / tm) * H * 3) / N)
elseif temp > tm then
temp = tm
end```

Even if the current temperature is below max, then the next increase could still push it over the limit - you would then calculate steam gain / fuel usage with that erroneous figure before correcting it back down to the "real" maximum on the following iteration.

To correct this, it could be re-written as just:

```temp = temp + (H + (((tm - temp) / tm) * H * 3) / N)
if temp > tm then temp = tm end```

There may be other errors - I've no experience with boilers, so I have no idea what sort of numbers you'd be plugging in nor expecting out of it. But that block otherwise appears ok to me.

I can't see the relation between your "cooling" system and CJ's code - that looks entirely different? I'm also not seeing where you got the steam production formula from?

Paramecium13, on 11 February 2014 - 09:46 PM, said:

Note: please remove the space after the b's in parenthesis. I had to put them in so this editor would not mess them up.
Disabling emoticons when posting avoids that particular issue.

Paramecium13, on 11 February 2014 - 09:46 PM, said:

The variable "tm" is the maximum temperature of the boiler (1000 for high pressure and 500 for low pressure). The variables H, EH, EP, FPBC, sigma, and N are the Heat Step, fuel heat inefficiency, fuel pressure inefficiency, fuel per boiler cycle, number of tanks, and steam per unit water.
Any particular reason you didn't use more descriptive variables directly within your code? Reading it... wasn't fun. :| (case in point - you got this legend wrong!)

### #3Forecaster

• Members
• 12 posts

Posted 12 February 2014 - 04:53 PM

Paramecium13, on 11 February 2014 - 09:46 PM, said:

(though I noticed it did not implement the fuel value changes outlined here)
Uhhh... oops

*fixes*

### #4Paramecium13

• New Members
• 2 posts

Posted 12 February 2014 - 05:49 PM

Thank you for your help. Your improvement to my code should help reduce errors at higher temperatures, though most of the time my code comes up with a much lower temperature. The reason my cooling down code looks so different is that I replaced some of the expressions with the variable 'tvar' to simplify the code. I used short expressions for my variables because I originally worked on this with pencil and paper. I used recursive formulas, difference equations, etc. to try to find explicit formula for the steam, temperature, etc. However, I could not find an explicit formula for the amount of time it would run for using a CAS so I switched to writing the above code (the original version is still on paper). When I am able to get my original work, I will try to take a different approach, probably using Newton's Method. I could also make an approximation using the trendline feature in Excel, though I would have to input a lot of data and use a different function for each set of options (i.e. hp/lp, number of tanks).

#### 1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users