
Gravitational energy
I just wanted to check and see if I've understood this right.
The total gravitational energy of a set of massive bodies is the sum of their individual kinetic energy and their individual potential energy. The kinetic energy is relative to some common point of reference and the potential energy is for each body relative to its parent body.
That doesn't seem quite right for the potential energy though. Shouldn't the potential energy be calculated with respect to the same reference point as the kinetic energy?

It's the total orbital energy that is equal to
Where r and v are both measured relative to M.

Somewhat related to your question is the virial theorem.

So how do you calculate the kinetic and potential energy of a three body system? For example, if you just restrict yourself to the Earth, Moon and Sun, the above formula gives the total orbital energy of the Moon around the Earth, and the Earth around the Sun, but how do you combine those? The virial theorem relates the average kinetic and potential energy, but to apply Hamiltonian mechanics, I need the instantaneous energies.

I wasn't suggesting the virial theorem as a solution to your specific problem. However, it does provide information about the manybody problem that doesn't require an exact solution.

I've been trying to puzzle this out myself. I'm pretty sure is the correct kinetic energy for a system of gravitating particles, and I'm pretty sure that the potential energy would be the energy required to infinitely separate all the particles. Now, I'm not exactly sure how to compute that, but I think it works out to which would be something like the energy required to separate the particles one at a time. I still need to do some sanity checks on that though (like making sure the order of separation doesn't matter) and then check that the total energy is a constant.

Well, if I've understood what I've been reading...
Then...
Where is the x component of the position of particle i, is the x component of the momentum of particle i, and (as expected).
Now, the first derivative there makes perfect sense, but I don't know whether or not the second one does. When implementing typical gravity simulations, I would normally end up with on the bottom. I'm also not too sure how to test some of this unless I can find a closed form 3body instance. (Maybe 2body would be good enough just to confirm H is a constant...)