My tinkering with ActionScript workers continues with the implementation of a simple particle system with mutual gravitational attraction. Mutual gravitation is computationally expensive because, in a naïve implementation like mine, every particle needs to reference every other in each simulation step. There are techniques such as neighbour searching to optimise this, but I'm idea rich and time poor, so I haven't implemented any proper optimisations.

In my application, I've created two background workers to handle the calculation of particle velocities and positions: ParticleCalculatorWorker and another to handle rendering: ParticleRendererWorker. After each step of ParticleCalculatorWorker, the updated particle vector is passed back to the user interface and handed to the renderer. Both the calculator and the render run then simultaneously with the former calculating the next step while the latter renders the current data set.

 The mutual gravitation code is based on Richard Lord's Flint Particles - a really nice particle system for Flex that I've played with a lot in the past.

The main UI contains two spark line widgets that give a relative indication of how long the two background workers are taking. I used the lightweight and easy to implement Sparkline components from Scott Logic for these.

The final results are, dare I say, rather nice. I was inspired by the work at the UK Astrophysical Fluids Facility and despite them having 100,000 CPU hours on 64 processors, there are some similarities in the patterns that develop. With a well populated particle field, small clusters develop and as they get denser the orbits become unstable and particles get ejected at high speed.

On my elderly MacBook Pro, I can run happily at about 200 particles (40,000 calculations per step). I've put an upper bound of 1,000 particles on the UI (a million calculations per step!). The great thing about workers though is that I can set that upper bound in the UI and even if the worker is choking on handling that many calculations, the UI is still responsive.

All in all, another success for ActionScript Workers! Next mini-project is to use workers for fluid dynamics.

The source code for the particles worker demo is available here and the application itself is available here. The application requires FlashPlayer 11.4.
  
0

Add a comment

About Me
About Me
Labels
Labels
Blog Archive
Loading