-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Runge-Kutta Integration #6
Comments
Hey jacione, thank you for your interesting message! You mention a very good point. The app should definitely offer other integration methods, not just Euler. As far as I can see, RK just takes more time per particle, but doesn't change the computational complexity, which is perfect. Let's say each particle has
Those values ( A simulation step consists of two parts
For each particle, the algorithm calls an "acceleration" method So in a first step, the velocities are changed based on In the second step, the positions are changed based on Does RK4 even fit into this framework? The main point of confusion for me is currently that we need to query the change at different time steps. In the wikipedia article you linked, The acceleration depends on the other particles' positions. So maybe, in order to compute (This seems expensive, but I can think of a few possible ways to optimize this.) Does this make sense or am I on the wrong path here? Maybe you can find time to write out how you would implement RK4. That would be incredibly helpful. |
Ah, is it possible that my thoughts about "looking into the future" were just way too complicated? Can I maybe just leave out the time parameter |
Actually, looking into the future is exactly what RK is all about :) The forces don't explicitly depend on time, but they do depend on the overall state of the system, which develops in time. So, when the definition refers to Having a black-box accelerator is not a problem; in fact it's what these methods were made for. The variable Ok, so for a single RK4 step, we're basically calculating four slightly modified Euler steps and then taking a weighted average. I am by no means an expert in this, but here's how I would do that in pseudocode:
This is approximately 4 times as much work as the Euler method. It also takes up more memory, since you're holding onto all these intermediate steps. However, as you pointed out, it doesn't affect the way the algorithm scales, which is really nice. The benefit is that the steps are much more accurate and stable, allowing you to take significantly larger steps. In the end, it can actually end up being cheaper to simulate the same total amount of time. |
I just realized something else. The equation of motion for a constantly accelerating body is: The way this is currently implemented is missing that factor
This doesn't seem to have broken anything so far, but it does change the way I would implement the RK integrator:
I also changed the FP divisions to multiplications where it could be done exactly. |
Hi Nick, awesome, thanks for the explanation! I've made several attempts to implement this in Particle Life now. Btw, I think in your pseudo code, it should be For some reason though, the tradeoff you mentioned doesn't work for me. Additionally, my RK4 implementation introduces some artifacts (only for larger dt) that I didn't see before with Euler integration. So maybe there is still a mistake in how we're thinking about the interplay of x, v and a? If you're interested, I opened a pull request where you can see the code I wrote for this: #7 |
Ah yes, I see my mistake. Good catch! I looked at your code and did some searching on Stack Exchange, and I think I see the problem. As I understand it, the I'm not sure if I'm describing this well. I'll see if I can get some pseudocode worked out... |
Yes, I had the exact same thought. It would take some additional effort to implement it though, and might again not improve stability enough. |
Totally understand. Maybe I'll finally get around to learning some Java after all. I've got some time over the holidays... :) |
I made a PR #8 with my own attempt. It's totally untested, and you should definitely treat it like pseudocode, but it might be a good place to start! |
Awesome! I'll try to look at it as soon as possible, but not before next week. |
I love your particle life GUI. In particular, the 1/r and 1/r^2 attractors are an incredibly intuitive way to simulate material properties like emergent crystal formation, etc.
However, certain regions of parameter space cause the simulation to become metastable or unstable for these attractors. I haven't taken the time to find the exact limits, but it seems to be a combination of:
As far as I can tell, this occurs because the inter-particle potential goes to infinity as the distance goes to zero, and because the force is assumed to be constant between timesteps (I assume--I'm not familiar enough with Java to know for sure, but Euler methods are usually associated with this kind of instability). For small distances, this will always underestimate the force between two approaching particles and overestimate the force between two diverging particles. In such a case, each interaction actually adds energy to the system.
This can still be stable as long as that extra energy is dissipated before the next interaction, which depends on both the particle density (mean free path) and the friction. Otherwise, it leads to a chain reaction (which is actually another interesting physics simulation).
The simplest fix to this would probably be to limit the interatomic potential function so that it doesn't actually go to infinity. However, while this would probably expand the stable region, it doesn't actually solve the problem entirely.
Alternatively, you could adjust the way the positions are calculated to use a Runge-Kutta method. This would be significantly more stable and would let you keep the inter-particle potentials true to their names. It would be more expensive per-step, but it would also be able to take larger (i.e. fewer) steps. In my experience, RK methods typically come out ahead on that tradeoff. It would also be more work for you, so there's that to consider :P
Once again, I love the project. Keep up the good work!
The text was updated successfully, but these errors were encountered: