3 <script src=
"../htmlrunner.js"></script>
5 /* The Great Computer Language Shootout
6 http://shootout.alioth.debian.org/
7 contributed by Isaac Gouy */
9 var PI
= 3.141592653589793;
10 var SOLAR_MASS
= 4 * PI
* PI
;
11 var DAYS_PER_YEAR
= 365.24;
13 function Body(x
,y
,z
,vx
,vy
,vz
,mass
){
23 Body
.prototype.offsetMomentum = function(px
,py
,pz
) {
24 this.vx
= -px
/ SOLAR_MASS
;
25 this.vy
= -py
/ SOLAR_MASS
;
26 this.vz
= -pz
/ SOLAR_MASS
;
32 4.84143144246472090e+00,
33 -1.16032004402742839e+00,
34 -1.03622044471123109e-01,
35 1.66007664274403694e-03 * DAYS_PER_YEAR
,
36 7.69901118419740425e-03 * DAYS_PER_YEAR
,
37 -6.90460016972063023e-05 * DAYS_PER_YEAR
,
38 9.54791938424326609e-04 * SOLAR_MASS
44 8.34336671824457987e+00,
45 4.12479856412430479e+00,
46 -4.03523417114321381e-01,
47 -2.76742510726862411e-03 * DAYS_PER_YEAR
,
48 4.99852801234917238e-03 * DAYS_PER_YEAR
,
49 2.30417297573763929e-05 * DAYS_PER_YEAR
,
50 2.85885980666130812e-04 * SOLAR_MASS
56 1.28943695621391310e+01,
57 -1.51111514016986312e+01,
58 -2.23307578892655734e-01,
59 2.96460137564761618e-03 * DAYS_PER_YEAR
,
60 2.37847173959480950e-03 * DAYS_PER_YEAR
,
61 -2.96589568540237556e-05 * DAYS_PER_YEAR
,
62 4.36624404335156298e-05 * SOLAR_MASS
68 1.53796971148509165e+01,
69 -2.59193146099879641e+01,
70 1.79258772950371181e-01,
71 2.68067772490389322e-03 * DAYS_PER_YEAR
,
72 1.62824170038242295e-03 * DAYS_PER_YEAR
,
73 -9.51592254519715870e-05 * DAYS_PER_YEAR
,
74 5.15138902046611451e-05 * SOLAR_MASS
79 return new Body(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, SOLAR_MASS
);
83 function NBodySystem(bodies
){
88 var size
= this.bodies
.length
;
89 for (var i
=0; i
<size
; i
++){
90 var b
= this.bodies
[i
];
96 this.bodies
[0].offsetMomentum(px
,py
,pz
);
99 NBodySystem
.prototype.advance = function(dt
){
100 var dx
, dy
, dz
, distance
, mag
;
101 var size
= this.bodies
.length
;
103 for (var i
=0; i
<size
; i
++) {
104 var bodyi
= this.bodies
[i
];
105 for (var j
=i
+1; j
<size
; j
++) {
106 var bodyj
= this.bodies
[j
];
107 dx
= bodyi
.x
- bodyj
.x
;
108 dy
= bodyi
.y
- bodyj
.y
;
109 dz
= bodyi
.z
- bodyj
.z
;
111 distance
= Math
.sqrt(dx
*dx
+ dy
*dy
+ dz
*dz
);
112 mag
= dt
/ (distance
* distance
* distance
);
114 bodyi
.vx
-= dx
* bodyj
.mass
* mag
;
115 bodyi
.vy
-= dy
* bodyj
.mass
* mag
;
116 bodyi
.vz
-= dz
* bodyj
.mass
* mag
;
118 bodyj
.vx
+= dx
* bodyi
.mass
* mag
;
119 bodyj
.vy
+= dy
* bodyi
.mass
* mag
;
120 bodyj
.vz
+= dz
* bodyi
.mass
* mag
;
124 for (var i
=0; i
<size
; i
++) {
125 var body
= this.bodies
[i
];
126 body
.x
+= dt
* body
.vx
;
127 body
.y
+= dt
* body
.vy
;
128 body
.z
+= dt
* body
.vz
;
132 NBodySystem
.prototype.energy = function(){
133 var dx
, dy
, dz
, distance
;
135 var size
= this.bodies
.length
;
137 for (var i
=0; i
<size
; i
++) {
138 var bodyi
= this.bodies
[i
];
140 e
+= 0.5 * bodyi
.mass
*
141 ( bodyi
.vx
* bodyi
.vx
142 + bodyi
.vy
* bodyi
.vy
143 + bodyi
.vz
* bodyi
.vz
);
145 for (var j
=i
+1; j
<size
; j
++) {
146 var bodyj
= this.bodies
[j
];
147 dx
= bodyi
.x
- bodyj
.x
;
148 dy
= bodyi
.y
- bodyj
.y
;
149 dz
= bodyi
.z
- bodyj
.z
;
151 distance
= Math
.sqrt(dx
*dx
+ dy
*dy
+ dz
*dz
);
152 e
-= (bodyi
.mass
* bodyj
.mass
) / distance
;
158 window
.onload = function(){ startTest("sunspider-access-nbody", '38cd3387');
162 test( "N-Body", function(){
163 for ( var n
= 3; n
<= 6; n
*= 2 ) {
164 var bodies
= new NBodySystem( Array(
165 Sun(),Jupiter(),Saturn(),Uranus(),Neptune()
169 ret
= bodies
.energy();
170 for (var i
=0; i
<max
; i
++){
171 bodies
.advance(0.01);
173 ret
= bodies
.energy();