Efficient Gravitational Field Implementation

I am basically looking for an efficient way to implement gravitational fields.

I have a huge 2D space, with thousands of objects in it. I then need to simulate how these objects are effected by each other's gravity.

I thought it'd be possible to sort the objects into collections, and check every object outside that collection against that collection, and not every individual object inside of it. I soon came to the realization that this simply wasn't possible. The gravitational field of multiple objects cannot represented as one uniform field, calculated with only one mass and distance.

Every object inside the simulation can be considered a sphere. I am fine with approximations, as long as it looks reasonably realistic.

-

You could try using a library that implements the Fast Multipole Method (FMM), which should drastically reduce the amount of memory you need and will decrease the complexity of matrix-vector products from $\mathcal{O}(N^{2})$ to $\mathcal{O}(N)$. It is difficult to implement, but there should be some libraries out there.

Another algorithm for N-body simulation is Barnes-Hut, which is easier to implement, and probably also has library implementations available. It is considered less efficient (in the asymptotic sense) than FMM.

-
I've done some reading about it already, and it appears to group elements. As all my elements will be fairly close to each other, and the masses being dealt with are rather small, will this still be accurate? –  Jeroen Bollen Mar 24 '14 at 18:56
I don't have any experience using it; I've only attended a few lectures on the method. FMM is supposed to be robust, and the general idea is that the order of the summation is truncated once it has attained sufficient accuracy (error bounds can be derived for the summation, so these are used for the accuracy test). Also, the accuracy is not supposed to be source-distribution-dependent. FMM is supposed to be more efficient than particle mesh Ewald when sources are close together. –  Geoff Oxberry Mar 24 '14 at 19:04
While every individual mass has a monopole field associated with it, a sum of masses does not have a pure monopole field. The point of FMM is that this sum of fields can be efficiently represented by a (relatively low order) multipole field since the higher modes of the field decay quickly with distance. –  Wolfgang Bangerth Mar 24 '14 at 19:37
If I were to only use the field for the purpose of calculating acceleration, would such a limitation open up a new scope of possible ways to do it? –  Jeroen Bollen Mar 24 '14 at 21:57