aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRahiel Kasim <rahiel@protonmail.ch>2015-11-02 17:30:02 +0100
committerRahiel Kasim <rahiel@protonmail.ch>2015-11-02 17:30:02 +0100
commit8712d27e03fc868d6bfdc488e15305b31216c3a8 (patch)
treeaa500419dac675e74ee1ba68b21afa59da6e619b
parentd3bea42b8ebdd7e9e961a1108032a223b7d6cbd4 (diff)
rework leapfrog integration
-rw-r--r--calc.js23
-rw-r--r--main.js4
-rw-r--r--systems.js4
3 files changed, 19 insertions, 12 deletions
diff --git a/calc.js b/calc.js
index 184ddb5..f28dd09 100644
--- a/calc.js
+++ b/calc.js
@@ -46,15 +46,26 @@ export function symplectic_euler(b, h) {
}
+export function leapfrog_initial(b, h) {
+ // kickoff the leapfrog integrator by computing v_{1/2} from v_0
+ for (let i = 0; i < b.length; i++) {
+ b[i].v.add(accel(i, b).multiplyScalar(.5 * h));
+ }
+
+ return b;
+}
+
export function leapfrog(b, h) {
- let bodies = [];
+ // leapfrog integration from x_0 and v_{1/2}
for (let i = 0; i < b.length; i++) {
- let r = b[i].r.clone().add(b[i].v.clone().multiplyScalar(h));
- let v = b[i].v.clone().add(accel(i, b).multiplyScalar(.5*h));
- bodies.push(b[i].clone().set(r, v));
+ b[i].r.add(b[i].v.clone().multiplyScalar(h));
}
- return bodies;
+ for (let i = 0; i < b.length; i++) {
+ b[i].v.add(accel(i, b).multiplyScalar(h));
+ }
+
+ return b;
}
export function getGravCenter(b) {
@@ -68,7 +79,7 @@ export function getGravCenter(b) {
return gravCenter.divideScalar(totMass);
}
-export function removeLostBodies(b, spheres, scene, range){
+export function removeLostBodies(b, spheres, scene, range) {
let gravCent = getGravCenter(b);
for (let i = 0; i < b.length; i++) {
let pos = b[i].r.clone();
diff --git a/main.js b/main.js
index 7812e70..e14ca6b 100644
--- a/main.js
+++ b/main.js
@@ -290,7 +290,7 @@ function init() {
}
function animate_leapfrog() {
- bodies = calc.leapfrog(bodies, system.stepsize);
+ bodies = calc.leapfrog_initial(bodies, system.stepsize);
requestId = requestAnimationFrame(animate);
}
@@ -298,7 +298,7 @@ function animate() {
timer.setTime(timer.getTime() + (system.stepsize * system.stepsPerFrame * 1000));
for (let i = 0; i < system.steps; i ++) {
- bodies = calc.symplectic_euler(bodies, system.stepsize);
+ bodies = calc.leapfrog(bodies, system.stepsize);
}
[bodies, spheres] = calc.removeLostBodies(bodies, spheres, scene, system.boundary);
diff --git a/systems.js b/systems.js
index 9e2f96f..faa0403 100644
--- a/systems.js
+++ b/systems.js
@@ -192,7 +192,6 @@ export function gen2Bodies(sunOn) {
sunOn: sunOn,
sphereP: 32,
boundary: 2000
-
};
}
@@ -211,8 +210,6 @@ export function gen3Bodies(sunOn) {
sunOn: sunOn,
sphereP: 32,
boundary: 2000
-
-
};
}
@@ -238,7 +235,6 @@ export function genBodies(n, bodyTexture, sunOn, collisions) {
sunOn: sunOn,
sphereP: 12,
boundary: 2000
-
};
}