I'm looking for information on the train braking mechanic.

Don't know how to use a machine? Looking for efficient setups? Stuck in a mission?
mmmPI
Smart Inserter
Smart Inserter
Posts: 2749
Joined: Mon Jun 20, 2016 6:10 pm
Contact:

Re: I'm looking for information on the train braking mechanic.

Post by mmmPI »

I would say like farcast, that the friction no aero model is the most accurate. Not because "the lower delta" is better, but rather , the flatter the line is better. It's not super visible from the graphs because of ABS(delta) but with the signed value, the frictionless models sometimes predict too much sometimes too little, while in the process, being "perfect" in some cases, whereas the friction no aero model seem to have a more constant distance and trend compared to the precise ingame measurement.

By trend i mean it's slightly diverging when braking research increases. While being as mentionned constantly at a difference of at least 10.

The graphs i think clearly identify the aero model as wrong, especially the 1-0 where it's supposed to be the most impactful, is the one having the largest delta, and the better the breaking research, the less time/steps of divergence for the prediction so it looks more accurate but it's obviously not the correct formula.

I think the "more precise" result for the frictionless model with higher braking force are an artifact, just because the overall braking time is shorter, it gives less and less time for the "wrong" formula to shows it's the "wrong" formula as friction's impact is becoming increasingly marginal compared to the braking power coming from the research.

This because i think the frictionless model appear to neglect a phenomenon ( friction ), but it's not always visible because of another unknown one ( some adjustment ? ) as such when friction has low impact, it can be exactly balanced out by the other phenomenon and look 100% accurate but with more or less braking time, giving more or less time/Steps for the frictionless model to diverge ; the prediction is sometimes too much, sometimes not enough. As such i think the frictionless model is not accurate as a formula similar to aero whereas the friction model would be more accurate as a formula (over time or different conditions ) except it needs a little something more for more precision.

In my opinion i trusted the latest in game measurement the most.( they can validate/unvalidate models because exhaustive and precise ) I think it means that the shorter braking time/distance, the more difficult it is to predict what the game does ( as the "most trusted model" is the "friction no aero" for me) because the game is doing something like a nudging or an adjustment, which is made more visible when it's not given much time to hide it. ( better breaking research, making a (flat or bounded ) adjustment from the game more visible in the total % of difference compared to the prediction).

When looking at the first picture from Milo's last post, with the red and green line, i think some hypothesis can be made to describe the adjustment.

I suppose the game is made so that trains "knows" their braking distance (using the friction model). That's what we see with the debug tool as a line and how they know when to actually start braking. ( human drivers are supposed to know the braking distance of their car/truck in order to drive properly and stop at the correct spot and i don't see how to do otherwise in a model where train don't share block reserved for potential braking like racing driver in F1).

=> The train known when it needs to brake, then it start braking. The amount of time between the 2 action is either 0 or more ticks. If 0 ticks, it is very unlikely that the train using its full braking force would happen to end up at 0 speed at the exact place where it need to stop. Because time is made of discrete ticks, the train maybe one tick would end up a few pixel too early, and the next tick, a few pixel too late if using its full braking power.

= a hypothetic train going at full speed as a theorical braking distance of say 120.00000 tiles, but a speed of 1.2300 tiles/tick.
Tick 0, should brake ? distance to target = 123 tiles, no ,
Tick 1, should brake ? distance to target = 121.77 tiles, no ,
Tick 2, should brake ? distance to target = 120.54 tiles , yes ,
Tick 2 Need to start braking, tick 3 is too late. But tick 2 is a little too early, train will stop 0.54 tile off if using full braking power according to those precise math formula no ?

This is already a source of potential imperfection/adjustment that would be made relatively/significantly worse overall compared to the total amount of tick predicted from a prediction with a high braking force, because that would be a small amout of tick and as such the adjustement would in % be higher, which seems to be the trend in the graphs. ( initial offset slowly increasing with braking research always positive delta for imprecision )

Now of course it can be that train anticipate more than 1 tick their decision to start braking and that the adjustment account for the distance during that tick, and as such i suppose the imperfection would be overall greater for faster train but the measurement were all made with trains running at 1.38 tile/tick and i havent added this to my doc :(

TL DR : Friction no aero seem to me the more accurate model

TL DR hypothesis : braking distance and travel distance left might never be exact modulo. if trains takes a little more time to brake than what is predicted by the friction model it could be because the train first brake at its full power and then fill in the 0.54 tiles gap from the example leading to extra ticks at very slow speed just before the complete stop

farcast
Long Handed Inserter
Long Handed Inserter
Posts: 86
Joined: Fri Jul 06, 2018 8:25 am
Contact:

Re: I'm looking for information on the train braking mechanic.

Post by farcast »

So... trains start braking 1 meter before a rail signal, effectively adding ~1 meter to the braking distance and changing stop time accordingly.

The good news is: This doesn't happen when a train is stopping at a train stop.

The bad news is: Now there's a ~10 tick difference between calculated and measured stop time even for a 1-18 coal powered train.

I can't find a train & research bonus where the difference isn't about 10 ticks. I'm running out of ideas.
Efficient inefficient design.

mmmPI
Smart Inserter
Smart Inserter
Posts: 2749
Joined: Mon Jun 20, 2016 6:10 pm
Contact:

Re: I'm looking for information on the train braking mechanic.

Post by mmmPI »

farcast wrote:
Fri Oct 13, 2023 9:38 pm
So... trains start braking 1 meter before a rail signal, effectively adding ~1 meter to the braking distance and changing stop time accordingly.
The good news is: This doesn't happen when a train is stopping at a train stop.
On the one hand i'm tempted to ask how did you come to this conclusion with math and explanations, but on the other hand, last time i wasn't able to follow everything, so i can understand you'd want to avoid that :) Maybe you'll spot the differences with my tests.

My guess for signal vs train stop is because signal are only 1x1 entity wheras train stop are 2x2, so that a train can shoot for the middle of it unlike a signal where train has to anticipate a little. I used a train stop for my test.
farcast wrote:
Fri Oct 13, 2023 9:38 pm
The bad news is: Now there's a ~10 tick difference between calculated and measured stop time even for a 1-18 coal powered train.
According to my calculations , a 1-18 coal powered train has only enough fuel for 60000 ticks, during which it won't reach more than 15 km/h.Was it the speed at which you got a 10 ticks difference ?

At this speed the friction model predict a braking time of 19 ticks, and my (rough) measurements give also the same 18 or 19 ticks. With the margin of error of a little coal left in the train after it burned almost the 3 stacks when braking, it wasn't exactly at its 60K ticks speed but after 60K tick speed variation per tick is almost 0.

29 ticks of braking time would mean the train is at 23 km/h instead of 15km/h which i would have definitly noticed. Same as 29 or 9 instead of the measured 18 or 19.

I'm still working on the formulas because i don't want to have 60K lines in the .ods ; i did 20K before realizing trains could have 3 stack of fuel :( this is the linked version

i added acceleration modeling that's when i realied it would take forever for such train to reach max speed and i would instead wait in game for the train to be almost fuel empty at speed x64, and saw the speed was around 15km/h and not increasing fast at all x) So i added +0.003 speed in the doc to make sure the braking model has same input as what's visible in game. 15.0 km/h

I then put a train stop a little in front of the train while game was paused and tasked it to stop at the train stop instead of the far away temporary point targeted for constant acceleration.

Then saved the game, and monitors when the "braking line" would turn from green to red, and from this moment i measured the ticks and counted 18 before complete stop at 19.

Which is also what the friction model predict if you give it a 1-18 trains coal powered train running at a speed of 0.6942 tile per tick or around that value which correspond to 15km/h.

TL DR : i was not able to reproduce the 10 ticks difference you mention but the example is quite and extreme case and am quite rusty with those maths
1-18 train about to slow down 3.zip
(1.32 MiB) Downloaded 32 times
speedsimu2.ods
(1.89 MiB) Downloaded 36 times
1-18 coal powered train projected speed.png
1-18 coal powered train projected speed.png (8.73 KiB) Viewed 1168 times

farcast
Long Handed Inserter
Long Handed Inserter
Posts: 86
Joined: Fri Jul 06, 2018 8:25 am
Contact:

Re: I'm looking for information on the train braking mechanic.

Post by farcast »

mmmPI wrote:
Sat Oct 14, 2023 12:54 pm
TL DR : i was not able to reproduce the 10 ticks difference you mention
1-18 coal powered train:
Total mass: 20000 kilograms

Total friction: 34200 Newtons = 19 rolling stock * 1800 Newtons per rolling stock

Total braking force:
-No research bonus: 264600 Newtons = friction + 36000 Newtons per locomotive * 1 locomotive + 10800 Newtons per cargo wagon * 18 cargo wagons

-Max research bonus: 495000 Newtons = friction + 2 * (36000 Newtons per locomotive * 1 locomotive + 10800 Newtons per cargo wagon * 18 cargo wagons)

Max speed (with forward facing locomotive in front): 4 meters per second = 14.4 km/h
Note about max speed
Momentum = Mass * Velocity

Force = Momentum / Time
= Mass * Velocity / Time

Time = Mass * Velocity / Force


No research:
Time = 20000 * 4 / 264600
= ~0.30234 seconds
= ~18.14 ticks
Measured time: 26 ticks

Max research:
Time = 20000 * 4 / 495000
= ~0.16162 seconds
= ~9.7 ticks
Measured time: 19 ticks

Measurements are from when train state switches to ARRIVE_STATION to when it switches to WAIT_STATION, starting the count at 1 and including the first WAIT_STATION tick. Train state is viewable with show-debug-info-in-tooltips enabled.
Debug tooltip.png
Debug tooltip.png (218.37 KiB) Viewed 1133 times

The save you provided has max braking force research.


The extra meter when stopping at a signal can be seen visually:
Extra meter for rail signal.png
Extra meter for rail signal.png (335.16 KiB) Viewed 1133 times
Just one tick earlier and the stopping point would be just barely more than 1 meter from the final stop point.

This extra meter also affects when the signal turns yellow, so it must be exactly 1 meter otherwise it would affect my braking distance meter. If it's exactly 1 meter, I can say it was accidentally removed from the braking distance measurements along side train length.


Measuring stop time to when the position stops changing gets closer to the actual stop time, but there's still a few extra unexplained ticks.
Last edited by farcast on Tue Oct 17, 2023 1:54 am, edited 1 time in total.
Efficient inefficient design.

mmmPI
Smart Inserter
Smart Inserter
Posts: 2749
Joined: Mon Jun 20, 2016 6:10 pm
Contact:

Re: I'm looking for information on the train braking mechanic.

Post by mmmPI »

farcast wrote:
Mon Oct 16, 2023 7:22 pm
The save you provided has max braking force research.
That's it :( i didn't realized it was the case in the save due to /cheat all, it was not reported properly in the speed calculation which gave 14.4kmh/H and i shouldn't have approximated it to 15km/H. Using the correct value of 2 for cumulative breaking force effect gives a prediction of between 9 and 10. Which does not correspond to measured value by 10 ticks as you said.

Quite puzzling. I think i will focus on the case with train stops firsts, and assume signals are treated specially.

mmmPI
Smart Inserter
Smart Inserter
Posts: 2749
Joined: Mon Jun 20, 2016 6:10 pm
Contact:

Re: I'm looking for information on the train braking mechanic.

Post by mmmPI »

farcast wrote:
Mon Oct 16, 2023 7:22 pm
Getting the numbers for thrust and drag requires its own set of measurements and calculations that I'm not going to get into here.
I think you did it the hard way with measurements, but it may be possible to do using the fact that locomotive have 600 kw energy consumption which is 600 kg m2 s−3 and then 0.5 friction and 0.0075 air_resistance in some yet-to-precise units ? I mean i tried but i'm not sure if i fail because of poor math or missing a conversion somewhere, or if because i attempted something for which i was still missing data and didn't realize when trying to move around the equations. ( that's a long time since i did units analysis which i was never good at :) )
farcast wrote:
Sat Sep 30, 2023 10:13 pm
As the train runs through, the length from the rear of the train to the stopping point is measured with a mix of diagonal tracks and straight tracks, plus a constant from the bend in-between (8.55 + 0.5 root 2). Each multiple of root 2 from the diagonal tracks has a unique decimal measurement, and any remaining length is measured by the whole number length straight tracks. Rail signal spacing limits diagonal measurements to multiples of 2 root 2, and straight measurements to multiples of 2, but the idea is the same.
I'm not sure i'm understanding properly this part but i think it relate to the previous sentence that's the method used to do the measurements. I noticed in this thread :

viewtopic.php?p=594021

8.55 - sqrt(2)/2 = 7.84289321881 whereas it would seem that ingame the value is : 7.842081225095013

I don't think it could explain the constant discrepancy since i'm using another (less efficient) method than yours to predict time of braking and i'm still having that same 10 tick difference on a straight piece of rail.

But i'm curious now to know where this 8.55 - 2sqrt(2)/2 is coming from and if it is trully correct and/or in which context maybe it could help for the other topic :)

I think maybe comparing the last 60 value of speed in game and in model when braking to full stop and noting as you shown me the debug state could be interesting because i remember my old measurement giving non-linear results, whereas for deceleration the current model that has the 10 ticks difference which counts friction but no air resistance, should yield linear deceleration.

Method i used being just simulating every step. It could help to localize more precisely when it start to diverge between predicted speed/position and measured speed/position. But i don't have much ideas otherwise.

farcast
Long Handed Inserter
Long Handed Inserter
Posts: 86
Joined: Fri Jul 06, 2018 8:25 am
Contact:

Re: I'm looking for information on the train braking mechanic.

Post by farcast »

mmmPI wrote:
Tue Oct 17, 2023 11:27 pm
farcast wrote:
Mon Oct 16, 2023 7:22 pm
Getting the numbers for thrust and drag requires its own set of measurements and calculations that I'm not going to get into here.
I think you did it the hard way with measurements, but it may be possible to do using the fact that locomotive have 600 kw energy consumption which is 600 kg m2 s−3
The 600kw provided by the tooltip is both the wrong unit and the wrong number for modeling train acceleration (Edit: I suppose the number would be right if you used the unit for momentum, meaning 600 momentum is added every tick). If you actually try to use it like I did so long ago, you'll get acceleration curves that don't line up with what happens in game at all. What actually works is force, or thrust. Trains are rocket powered I guess. They run on rocket fuel so it makes sense. There should totally be thrusters on the locomotive sprites!

I made a bug report about the tooltip recently but the devs decided against changing it. I tried to be funny by taking it way too seriously but maybe that was a bad idea?
mmmPI wrote:
Tue Oct 17, 2023 11:27 pm
0.0075 air_resistance in some yet-to-precise units ?
You get this number if you take the number and unit I used for drag, convert seconds to ticks, and divide by 1000... for some reason. Precision concerns maybe. It's effectively multiplied by 1000 when it gets used so that last part doesn't matter. If you then divide by mass, and multiply by 1 tick, you get a %change in speed after 1 tick of constant drag force. This is how the number is used by the game, but the 1 tick multiplier is omitted because multiplying by one doesn't do anything and computers don't care about units.
mmmPI wrote:
Tue Oct 17, 2023 11:27 pm
I noticed in this thread :

viewtopic.php?p=594021

8.55 - sqrt(2)/2 = 7.84289321881 whereas it would seem that ingame the value is : 7.842081225095013
...are you saying reddit lied to me?! How could this be! ;~;

I fixed the numbers and blueprint in my post, but the difference is small enough that the conclusion didn't change.
mmmPI wrote:
Tue Oct 17, 2023 11:27 pm
It could help to localize more precisely when it start to diverge between predicted speed/position and measured speed/position.
Seems worth looking into to me.
Efficient inefficient design.

mmmPI
Smart Inserter
Smart Inserter
Posts: 2749
Joined: Mon Jun 20, 2016 6:10 pm
Contact:

Re: I'm looking for information on the train braking mechanic.

Post by mmmPI »

farcast wrote:
Wed Oct 18, 2023 6:56 pm
The 600kw provided by the tooltip is both the wrong unit and the wrong number for modeling train acceleration (Edit: I suppose the number would be right if you used the unit for momentum, meaning 600 momentum is added every tick).

I made a bug report about the tooltip recently but the devs decided against changing it. I tried to be funny by taking it way too seriously but maybe that was a bad idea?
I wish it was the reason of my failed attempt but looking at your bug report i realized there was other mistakes in my work. Still to me kw is related to second, so it would not make sense that it should be added every tick without dividing by 60 first. That one i don't consider it my mistake !

I don't think it was a bad idea to be overly serious as joke. It's good to know it exist somewhere, for modded trains or fuel, for signal-less junctions, for general understanding of train behavior, for curiosity and for fun even if it didn't lead to a change in the game formulas / display one can always get back to it when needed even it's not "right" from a physics standpoint.

farcast wrote:
Wed Oct 18, 2023 6:56 pm
...are you saying reddit lied to me?! How could this be! ;~;

I fixed the numbers and blueprint in my post, but the difference is small enough that the conclusion didn't change.
I was not sure if the number you used and the one on reddit were correct and representing something like an average of right and left turn distance, or something you come up with from mathing out a certain layout, or the supposed "exact" value used by the game, now i understand it's not the correct number for what it was used ,but it would have been very hard to find the exact number used by the game from math, little by little i understand the measurement method :)

In the other topic when i put the number from reddit the difference was called "ginormous" ! I realized later it was also the one you used.

But i wasn't expecting it to change conclusions here either given which decimals are changed, it would have required many tests, to have just 1 tick where the speed is rounded unexpectedly, or an impossibly long braking distance for human eye to notice.

I would feel more enclined to do the long testings enabled by very precise model if i had an hypothesis to test or a particular need for a project . I know those math can apply to jumpings trains from the mod renai transportation, which are hard to use because one need to be very precise in managing their speed when going on a ramp and their braking distance and overall momentum, and different trains react differently, it would give some spice to the further research :)

aaargha
Filter Inserter
Filter Inserter
Posts: 330
Joined: Wed Dec 07, 2016 8:35 am
Contact:

Re: I'm looking for information on the train braking mechanic.

Post by aaargha »

If anyone is interested in doing further tests on this in the future I made a small mod to make the measuring easier. The mod listens for train state changes and starts tracking trains that go into defines.train_state.arrive_station or defines.train_state.arrive_signal. While a train is tracked some simple characteristics are logged to a file called train_brake_measure_results.txt in the script-output folder. The tracking stops once the train changes to another state/is destroyed/similar.

The data logged has the following |-separated format:

game.tick|train.id|train.speed|train.pos.x|train.pos.y

The values for position are taken from the position of the "front" stock in the direction of travel.

To use the data, simply import it to you favorite flavor of spreadsheet program :)
Multiple trains can be tracked in parallel without issue as long as you sort the results by train.id first and game.tick second.

The mod is shared under the Unlicense so anyone is free to use/adapt it to their needs. If you want to log more data, simply add it to the measure_train() function.

Do note that it should not be used in normal gameplay, the mod will happily log data until your disk is full.
Attachments
train_brake_measure_results.txt
Example output from a test
(2.1 KiB) Downloaded 31 times
Train-brake-measure_0.1.0.zip
Not uploaded to the mod portal as it is not meant for use outside of testing like in this thread
(2.95 KiB) Downloaded 30 times

mmmPI
Smart Inserter
Smart Inserter
Posts: 2749
Joined: Mon Jun 20, 2016 6:10 pm
Contact:

Re: I'm looking for information on the train braking mechanic.

Post by mmmPI »

mmmPI wrote:
Thu Oct 05, 2023 3:27 pm
i don't know how to write a command to " Export the speed of a locomotive entity every tick in file".
aaargha wrote:
Tue Oct 31, 2023 8:46 pm
If anyone is interested in doing further tests on this in the future I made a small mod to make the measuring easier.
That's perfect, i'm interested in using the mod and looking at how it's done thank you !

There are plenty of mods on the portal that are tagged as "for personnal use only" , or hidden as "internals" maybe yours would please someone not following the english discussion on the forum, but still happy to run precise measurement :)

aaargha
Filter Inserter
Filter Inserter
Posts: 330
Joined: Wed Dec 07, 2016 8:35 am
Contact:

Re: I'm looking for information on the train braking mechanic.

Post by aaargha »

Cheers! I hope you're able to get some useful results!

The mod would need some additional work before it's ready for public distribution, at least a way to cap the output size would be needed; anyone finding it in this thread and manually installing it will be aware of the potential side effects and the limited intended context of use. As I don't really intend to do more work on this mod, I wouldn't be comfortable putting it on the portal when I won't support it and it can potentially cause issues for peoples whole systems.

Post Reply

Return to “Gameplay Help”