• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* The Great Computer Language Shootout
2   http://shootout.alioth.debian.org/
3   contributed by Isaac Gouy */
4
5var PI = 3.141592653589793;
6var SOLAR_MASS = 4 * PI * PI;
7var DAYS_PER_YEAR = 365.24;
8
9function Body(x,y,z,vx,vy,vz,mass){
10   this.x = x;
11   this.y = y;
12   this.z = z;
13   this.vx = vx;
14   this.vy = vy;
15   this.vz = vz;
16   this.mass = mass;
17}
18
19Body.prototype.offsetMomentum = function(px,py,pz) {
20   this.vx = -px / SOLAR_MASS;
21   this.vy = -py / SOLAR_MASS;
22   this.vz = -pz / SOLAR_MASS;
23   return this;
24}
25
26function Jupiter(){
27   return new Body(
28      4.84143144246472090e+00,
29      -1.16032004402742839e+00,
30      -1.03622044471123109e-01,
31      1.66007664274403694e-03 * DAYS_PER_YEAR,
32      7.69901118419740425e-03 * DAYS_PER_YEAR,
33      -6.90460016972063023e-05 * DAYS_PER_YEAR,
34      9.54791938424326609e-04 * SOLAR_MASS
35   );
36}
37
38function Saturn(){
39   return new Body(
40      8.34336671824457987e+00,
41      4.12479856412430479e+00,
42      -4.03523417114321381e-01,
43      -2.76742510726862411e-03 * DAYS_PER_YEAR,
44      4.99852801234917238e-03 * DAYS_PER_YEAR,
45      2.30417297573763929e-05 * DAYS_PER_YEAR,
46      2.85885980666130812e-04 * SOLAR_MASS
47   );
48}
49
50function Uranus(){
51   return new Body(
52      1.28943695621391310e+01,
53      -1.51111514016986312e+01,
54      -2.23307578892655734e-01,
55      2.96460137564761618e-03 * DAYS_PER_YEAR,
56      2.37847173959480950e-03 * DAYS_PER_YEAR,
57      -2.96589568540237556e-05 * DAYS_PER_YEAR,
58      4.36624404335156298e-05 * SOLAR_MASS
59   );
60}
61
62function Neptune(){
63   return new Body(
64      1.53796971148509165e+01,
65      -2.59193146099879641e+01,
66      1.79258772950371181e-01,
67      2.68067772490389322e-03 * DAYS_PER_YEAR,
68      1.62824170038242295e-03 * DAYS_PER_YEAR,
69      -9.51592254519715870e-05 * DAYS_PER_YEAR,
70      5.15138902046611451e-05 * SOLAR_MASS
71   );
72}
73
74function Sun(){
75   return new Body(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, SOLAR_MASS);
76}
77
78
79function NBodySystem(bodies){
80   this.bodies = bodies;
81   var px = 0.0;
82   var py = 0.0;
83   var pz = 0.0;
84   var size = this.bodies.length;
85   for (var i=0; i<size; i++){
86      var b = this.bodies[i];
87      var m = b.mass;
88      px += b.vx * m;
89      py += b.vy * m;
90      pz += b.vz * m;
91   }
92   this.bodies[0].offsetMomentum(px,py,pz);
93}
94
95NBodySystem.prototype.advance = function(dt){
96   var dx, dy, dz, distance, mag;
97   var size = this.bodies.length;
98
99   for (var i=0; i<size; i++) {
100      var bodyi = this.bodies[i];
101      for (var j=i+1; j<size; j++) {
102         var bodyj = this.bodies[j];
103         dx = bodyi.x - bodyj.x;
104         dy = bodyi.y - bodyj.y;
105         dz = bodyi.z - bodyj.z;
106
107         distance = Math.sqrt(dx*dx + dy*dy + dz*dz);
108         mag = dt / (distance * distance * distance);
109
110         bodyi.vx -= dx * bodyj.mass * mag;
111         bodyi.vy -= dy * bodyj.mass * mag;
112         bodyi.vz -= dz * bodyj.mass * mag;
113
114         bodyj.vx += dx * bodyi.mass * mag;
115         bodyj.vy += dy * bodyi.mass * mag;
116         bodyj.vz += dz * bodyi.mass * mag;
117      }
118   }
119
120   for (var i=0; i<size; i++) {
121      var body = this.bodies[i];
122      body.x += dt * body.vx;
123      body.y += dt * body.vy;
124      body.z += dt * body.vz;
125   }
126}
127
128NBodySystem.prototype.energy = function(){
129   var dx, dy, dz, distance;
130   var e = 0.0;
131   var size = this.bodies.length;
132
133   for (var i=0; i<size; i++) {
134      var bodyi = this.bodies[i];
135
136      e += 0.5 * bodyi.mass *
137         ( bodyi.vx * bodyi.vx
138         + bodyi.vy * bodyi.vy
139         + bodyi.vz * bodyi.vz );
140
141      for (var j=i+1; j<size; j++) {
142         var bodyj = this.bodies[j];
143         dx = bodyi.x - bodyj.x;
144         dy = bodyi.y - bodyj.y;
145         dz = bodyi.z - bodyj.z;
146
147         distance = Math.sqrt(dx*dx + dy*dy + dz*dz);
148         e -= (bodyi.mass * bodyj.mass) / distance;
149      }
150   }
151   return e;
152}
153
154var ret;
155
156for ( var n = 3; n <= 24; n *= 2 ) {
157    (function(){
158        var bodies = new NBodySystem( Array(
159           Sun(),Jupiter(),Saturn(),Uranus(),Neptune()
160        ));
161        var max = n * 100;
162
163        ret = bodies.energy();
164        for (var i=0; i<max; i++){
165            bodies.advance(0.01);
166        }
167        ret = bodies.energy();
168    })();
169}
170