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