Energy anomolies in many body simulation - Computational Science Stack Exchange most recent 30 from scicomp.stackexchange.com 2022-01-27T02:07:23Z https://scicomp.stackexchange.com/feeds/question/8119 https://creativecommons.org/licenses/by-sa/4.0/rdf https://scicomp.stackexchange.com/q/8119 0 Energy anomolies in many body simulation user1750289 https://scicomp.stackexchange.com/users/3939 2013-07-31T14:23:56Z 2013-07-31T14:23:56Z <p>I am trying to simulate the gravitational interaction between many bodies. I am using a direct PP force calculation and a 4th order symplectic integrator with a variable step size. </p> <p>The energy of the simulation is calculated by this function:</p> <pre><code>real energy(body* system,int n){ int i,k,j; real energy_var=0; for(i=0;i&lt;n;i++){ energy_var += 0.5*system[i].mass*pow(mag(system[i].vel),2); for(j=i+1;j&lt;n;j++){ if(i==j){continue;} real position_vect={0,0,0}; for(k=0;k&lt;3;k++){ position_vect[k] = system[i].pos[k] - system[j].pos[k];} real distance = mag(position_vect); energy_var += -Gravitational_constant*system[i].mass*system[j].mass/distance; } } return energy_var;} </code></pre> <p>In a test run with 100 bodies the energy( calculated every 100000's ) appears to be constant apart from one anomaly. </p> <pre><code>Energy = -3.23197e+41 Energy = -3.23197e+41 Energy = -3.23197e+41 Energy = -4.24807e+41 Energy = -3.23197e+41 Energy = -3.23197e+41 Energy = -3.23197e+41 Energy = -3.23197e+41 Energy = -3.23197e+41 Energy = -3.23197e+41 </code></pre> <p>If it was a numerical or coding error i.e. the timestep being too high then I would not expect the energy to return to the same constant value. Is there a known phenomenon that might cause such an error? </p>