I am writing a simple computer simulation of a system (in 2 dimensions), with several circular bodies. They all affect each other by their gravity and some of them are connected via damped springs (one body can be connected to multiple springs). Springs are massless themselves, they just connect to objects with mass. Springs should heat up due to damping and distribute that heat to two masses spring connects.
I wanted to find out how much energy goes into the heat of each body because of damped springs.
I thought I could just subtract energy (kinetic and potential) before and after the integration step, and use that as heat energy. However, I can only calculate gravitational energy of the whole system (using gravitational potential), and I don't know how to split that energy among all masses (or springs), so that the sum of kinetic and potential energy is constant for a non-damped spring (maybe it is impossible to do so).
Also, it is not obvious how to split resulting heat energy of a spring among its two bodies (evenly or weighted).
So, how do I find the amount of energy which goes to heat, per each body, in a system of multiple bodies connected via damped springs in a gravity field?
I calculate gravity forces for each mass pair via
$$\overrightarrow{F_{g,i,j}}=-G \frac{m_i m_2 \overrightarrow{n_{ij}}}{|\overrightarrow{p_j}-\overrightarrow{p_i}|^2}$$
And spring forces between some mass pairs via
$$\overrightarrow{F_{s,i,j}}= \overrightarrow{n_{ij}} ( k_{ij} ( l_{ij} - |\overrightarrow{p_j}-\overrightarrow{p_i}| ) + b_{ij}( (\overrightarrow{v_j} - \overrightarrow{v_i}) \cdot \overrightarrow{n_{ij}}))$$
where $\overrightarrow{n_{ij}}= \frac{\overrightarrow{p_j}-\overrightarrow{p_i}}{|\overrightarrow{p_j}-\overrightarrow{p_i}|}$; ${p_i}$ - position of mass with index $i$; $\overrightarrow{v_i}$ - velocity of mass with index $i$; $l_{ij}$ - rest length of spring between masses $i$ and $j$; $G$, $k_{ij}$ and $b_{ij}$ - constants.
I use symplectic integrator of order 3 and update positions and velocities by timesteps.
Calculating heat energy would be easy if there would be no gravity by subtracting this after and before integration step:
$E_{t,i,j}=E_{k,i,j}+E_{p,i,j}=\frac{m_i |\overrightarrow{v_i}|^2+m_j |\overrightarrow{v_i}|^2}{2}+\frac{k_{ij} ( l_{ij} - |\overrightarrow{p_j}-\overrightarrow{p_i}| )^2}{2}$
But with gravity, kinetic energy is split between potential gravitational energy and potential spring energy, and I don't know how how to calculate gravitational energy of one mass (or two from spring). Also, one mass being connected via several springs complicates this as well.
EDIT for close voters: this is not homework, it is my personal project. And I am not asking to check my work, I am asking about physics concept - how to calculate heat energy of a damped spring, which I am unable to find answer anywhere.