N-Body Simulation

Published on Monday, April 24, 2023

Small week-end project!

The idea with this one was to create a n-body particles simulation in Minecraft using the minimum resources to run it. That is, running it within a Θ(n)=n(n1)2\Theta(n) = \frac{n(n-1)}{2} complexity (triangular).

So I got started with small planets i could generate summon with different weights and sizes ti see how the influence themselves.

Here are few notes about this project:

Limited precision

Despite using only Newton's formula which I thought would be fairly simple and wouldn't git any lack of precision due to its simplicity, going further than 2000 blocks from (0, 0) started creating int overflows.

This is due to the fact that I used a 10310^3 fixed point, which was required as I noticed that 10210^2 tends to lead to instabillity and reach singularities. This may happens to the 10310^3 model too, so I also implemented a cap to the jerk (rate of change of the acceleration).

I could have implemented instead a cap onto the acceleration, however i got better results in capping the jerk so it permitted to avoid placing any hard limit for the acceleration.

To come back about precision, with a fixed point value of 10310^3, it limited the project within a range of ±2000\pm2000 blocks:

X2109with X=xmaxPrecisionFixedPoint Precision=103(used for pre-division) FixedPoint=103 xmax2103\begin{aligned} & X \leq 2 * 10^9 &\text{with } X = x_{max} * P_{recision} * F_{ixed Point} \\\ & P_{recision} = 10^3 &\text{(used for pre-division)} \\\ & F_{ixed Point} = 10^3 \\\ & \Rarr x_{max} \leq 2 * 10^3 \end{aligned}

I could have increased this by doing the division first before multiplying back the element, but I would have lost precision for it.

MCFunction being MCfunction

Just a quick remark about how mcf is once more boring to execute what we want to do. Fab=Gmambdab2F_{ab} = G * \frac{m_a * m_b}{d_{ab}^2} actually takes up to 10 commands to be executed member per member, however once optimized, it can be done in only 6 operations.

scoreboard players operation .F_ab temp = .m_a temp
scoreboard players operation .F_ab temp *= .m_b temp
scoreboard players operation .F_ab temp /= #1000 const
scoreboard players operation .d_ab temp *= .d_ab temp
scoreboard players operation .F_ab temp *= #G const
scoreboard players operation .F_ab temp /= .d_ab temp

This is a very simple example with a low number of commands, but once optimized changing any of the parmameters (or adding new ones) might break the algorithm (or permit new optimizations).

I keep that in mind for a possible future project.