2 <!-- MOZ_INSERT_CONTENT_HOOK -->
3 <script src = runner.js
></script>
5 var onlyName
= 'N-Body', onlyNum
= 4;
8 /* The Great Computer Language Shootout
9 http://shootout.alioth.debian.org/
10 contributed by Isaac Gouy */
12 var PI
= 3.141592653589793;
13 var SOLAR_MASS
= 4 * PI
* PI
;
14 var DAYS_PER_YEAR
= 365.24;
16 function Body(x
,y
,z
,vx
,vy
,vz
,mass
){
26 Body
.prototype.offsetMomentum = function(px
,py
,pz
) {
27 this.vx
= -px
/ SOLAR_MASS
;
28 this.vy
= -py
/ SOLAR_MASS
;
29 this.vz
= -pz
/ SOLAR_MASS
;
35 4.84143144246472090e+00,
36 -1.16032004402742839e+00,
37 -1.03622044471123109e-01,
38 1.66007664274403694e-03 * DAYS_PER_YEAR
,
39 7.69901118419740425e-03 * DAYS_PER_YEAR
,
40 -6.90460016972063023e-05 * DAYS_PER_YEAR
,
41 9.54791938424326609e-04 * SOLAR_MASS
47 8.34336671824457987e+00,
48 4.12479856412430479e+00,
49 -4.03523417114321381e-01,
50 -2.76742510726862411e-03 * DAYS_PER_YEAR
,
51 4.99852801234917238e-03 * DAYS_PER_YEAR
,
52 2.30417297573763929e-05 * DAYS_PER_YEAR
,
53 2.85885980666130812e-04 * SOLAR_MASS
59 1.28943695621391310e+01,
60 -1.51111514016986312e+01,
61 -2.23307578892655734e-01,
62 2.96460137564761618e-03 * DAYS_PER_YEAR
,
63 2.37847173959480950e-03 * DAYS_PER_YEAR
,
64 -2.96589568540237556e-05 * DAYS_PER_YEAR
,
65 4.36624404335156298e-05 * SOLAR_MASS
71 1.53796971148509165e+01,
72 -2.59193146099879641e+01,
73 1.79258772950371181e-01,
74 2.68067772490389322e-03 * DAYS_PER_YEAR
,
75 1.62824170038242295e-03 * DAYS_PER_YEAR
,
76 -9.51592254519715870e-05 * DAYS_PER_YEAR
,
77 5.15138902046611451e-05 * SOLAR_MASS
82 return new Body(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, SOLAR_MASS
);
86 function NBodySystem(bodies
){
91 var size
= this.bodies
.length
;
92 for (var i
=0; i
<size
; i
++){
93 var b
= this.bodies
[i
];
99 this.bodies
[0].offsetMomentum(px
,py
,pz
);
102 NBodySystem
.prototype.advance = function(dt
){
103 var dx
, dy
, dz
, distance
, mag
;
104 var size
= this.bodies
.length
;
106 for (var i
=0; i
<size
; i
++) {
107 var bodyi
= this.bodies
[i
];
108 for (var j
=i
+1; j
<size
; j
++) {
109 var bodyj
= this.bodies
[j
];
110 dx
= bodyi
.x
- bodyj
.x
;
111 dy
= bodyi
.y
- bodyj
.y
;
112 dz
= bodyi
.z
- bodyj
.z
;
114 distance
= Math
.sqrt(dx
*dx
+ dy
*dy
+ dz
*dz
);
115 mag
= dt
/ (distance
* distance
* distance
);
117 bodyi
.vx
-= dx
* bodyj
.mass
* mag
;
118 bodyi
.vy
-= dy
* bodyj
.mass
* mag
;
119 bodyi
.vz
-= dz
* bodyj
.mass
* mag
;
121 bodyj
.vx
+= dx
* bodyi
.mass
* mag
;
122 bodyj
.vy
+= dy
* bodyi
.mass
* mag
;
123 bodyj
.vz
+= dz
* bodyi
.mass
* mag
;
127 for (var i
=0; i
<size
; i
++) {
128 var body
= this.bodies
[i
];
129 body
.x
+= dt
* body
.vx
;
130 body
.y
+= dt
* body
.vy
;
131 body
.z
+= dt
* body
.vz
;
135 NBodySystem
.prototype.energy = function(){
136 var dx
, dy
, dz
, distance
;
138 var size
= this.bodies
.length
;
140 for (var i
=0; i
<size
; i
++) {
141 var bodyi
= this.bodies
[i
];
143 e
+= 0.5 * bodyi
.mass
*
144 ( bodyi
.vx
* bodyi
.vx
145 + bodyi
.vy
* bodyi
.vy
146 + bodyi
.vz
* bodyi
.vz
);
148 for (var j
=i
+1; j
<size
; j
++) {
149 var bodyj
= this.bodies
[j
];
150 dx
= bodyi
.x
- bodyj
.x
;
151 dy
= bodyi
.y
- bodyj
.y
;
152 dz
= bodyi
.z
- bodyj
.z
;
154 distance
= Math
.sqrt(dx
*dx
+ dy
*dy
+ dz
*dz
);
155 e
-= (bodyi
.mass
* bodyj
.mass
) / distance
;
161 startTest("real-nbody");
165 for ( var n
= 2; n
<= 16; n
*= 2 ) (function(n
){
166 test( "N-Body", n
, function(){
167 var bodies
= new NBodySystem( Array(
168 Sun(),Jupiter(),Saturn(),Uranus(),Neptune()
172 ret
= bodies
.energy();
173 for (var i
=0; i
<max
; i
++){
174 bodies
.advance(0.01);
176 ret
= bodies
.energy();
183 <body onload=
"thisTest()"></body></html>