MCPcopy Create free account
hub / github.com/leaningtech/webvm / energy

Function energy

examples/nodejs/nbody.js:128–148  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

126}
127
128function energy() {
129 let e = 0;
130 const size = bodies.length;
131
132 for (let i = 0; i < size; i++) {
133 const bodyi = bodies[i];
134
135 e += 0.5 * bodyi.mass * ( bodyi.vx * bodyi.vx + bodyi.vy * bodyi.vy + bodyi.vz * bodyi.vz );
136
137 for (let j = i + 1; j < size; j++) {
138 const bodyj = bodies[j];
139 const dx = bodyi.x - bodyj.x;
140 const dy = bodyi.y - bodyj.y;
141 const dz = bodyi.z - bodyj.z;
142
143 const distance = Math.sqrt(dx * dx + dy * dy + dz * dz);
144 e -= (bodyi.mass * bodyj.mass) / distance;
145 }
146 }
147 return e;
148}
149
150const n = +50000000;
151

Callers 1

nbody.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected