post-kerbin-mining-corporation / NearFutureElectrical

Adds nuclear reactors and power utility parts to Kerbal Space Program
40 stars 36 forks source link

Uneven RTG decay rate #96

Closed KSAMissionCtrl closed 6 years ago

KSAMissionCtrl commented 6 years ago

so, played around with RTGs a bit more and I was having a lot of trouble figuring out how to calculate how long it would take for power to fall below a certain generation level - I thought I had the numbers right but when I jumped ahead that far in the game I was always off. So I decided to just brute-force it and write a simple kOS script that stared at the efficiency field and as soon as it dropped it would log the new amount and time-stamp it to a CSV file so I could just do a quick compare in Excel to get the time difference between decays. The results were interesting. The time between the decay is generally 25s but is interspersed with intervals of 50 seconds, however even when it takes twice as long to decay the rate of decay remains the same. This was done at 1x speed over a 30min period, no timewarp - although 1000x timewarp produced similar effects. The original charge amount of the RTG is 0.15 and the half-life is 9.2 years, with my game set to Earth time.

Everything but essentials was stripped out for testing after first discovering this with all my mods installed. output_log.txt

ChrisAdderley commented 6 years ago

I don't know how kOS works but the math is exponential decay. You can predict it with any version of this formula, the fastest implementation is to calculate Et = E0*2^(-t(years)/l(years)). With your numbers from your spreadsheet (t of 1846.52s, or 5.85527651e-5 years), and a half life of 9.2, I get a drop to 99.999558 which compares well to the numbers in the sheet.

The software recalculates this based on the total time delta. Because of computers and precision, that string floating field that displays efficiency is likely only updated when the rounding of efficiency fits into its precision. Because the decay is logarithmic, this rate is fully expected to be nonlinear.

To effectively predict how long it would take to drop below a level, rearrange the formula to predict t(years)... or exploit the definition of half life for a back of the envelope calculation:

Did a quick test this morning to confirm this behaviour works as expected and it seems to be pretty solid.

KSAMissionCtrl commented 6 years ago

I always find it funny how much I manage to do with this game and programming in general with such a horrid and utter lack of math skills. I hated math growing up, wish I had a game like KSP to properly apply myself when I was learning it! Number Crunchers was a nice focused game but that was about it :P I was looking up decay formulas and saw it was logarithmic but did not remember that meant nonlinear sigh Thanks for the refresher!

ChrisAdderley commented 6 years ago

No problem! Happy to help, and even happier to see people using the mod!

KSAMissionCtrl commented 6 years ago

fastest implementation is to calculate Et = E0*2^(-t(years)/l(years))

I just took a closer look at this now that it's no longer 3am and I'm a bit unsure of what some of the variables mean. E0 can't mean multiply by 0 because you're using a * sign for multiplication so is it a typo and supposed to be EO for Energy Output? I thought at first years was a variable but I think you were just saying that t and l should be in years? Not sure if you solved for Et or E0 to get your final result.

Gah. Ok for a practical example I'm looking to see how long it would take the power generation to drop below 130 Watts or 0.13EC/s

ChrisAdderley commented 6 years ago

Here we are with subscripts as I'm not on a phone: Et = E0 * 2-t/l You can do the math to rearrange it, or the easy way is to grab a calculator: http://www.calculator.net/half-life-calculator.html?type=1&nt=0.38&n0=1&t=&t12=9.32&x=47&y=26 Enter the variables (starting EC rate as N0 , ending EC as Nt, and enter the half life. In that link I entered a start EC as 1, so it takes 13y to get to that level.

KSAMissionCtrl commented 6 years ago

Thank you!