Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | // |
michael@0 | 2 | // NBody adapted from Intel's nbody benchmark. |
michael@0 | 3 | // |
michael@0 | 4 | |
michael@0 | 5 | load(libdir + "util.js"); |
michael@0 | 6 | load(libdir + "seedrandom.js"); |
michael@0 | 7 | |
michael@0 | 8 | var NBody = { |
michael@0 | 9 | Constant: { |
michael@0 | 10 | "deltaTime": 1, // 0.005 in their code. |
michael@0 | 11 | "epsSqr": 50, // softening factor, when they compute, set to 50. |
michael@0 | 12 | "initialVelocity": 8 // set to 0 to turn off |
michael@0 | 13 | }, |
michael@0 | 14 | |
michael@0 | 15 | init: function init(mode, numBodies) { |
michael@0 | 16 | var initPos = new Array(numBodies); |
michael@0 | 17 | var initVel = new Array(numBodies); |
michael@0 | 18 | |
michael@0 | 19 | // initialization of inputs |
michael@0 | 20 | for (var i = 0; i < numBodies; i++) { |
michael@0 | 21 | // [x,y,z] |
michael@0 | 22 | initPos[i] = [Math.floor((Math.random()) * 40000), |
michael@0 | 23 | Math.floor((Math.random()) * 20000), |
michael@0 | 24 | Math.floor((Math.random() - .25) * 50000)]; |
michael@0 | 25 | |
michael@0 | 26 | // [x,y,z,x,y,z] |
michael@0 | 27 | initVel[i] = [(Math.random() - 0.5) * NBody.Constant.initialVelocity, |
michael@0 | 28 | (Math.random() - 0.5) * NBody.Constant.initialVelocity, |
michael@0 | 29 | (Math.random()) * NBody.Constant.initialVelocity + 10, |
michael@0 | 30 | |
michael@0 | 31 | (Math.random() - 0.5) * NBody.Constant.initialVelocity, |
michael@0 | 32 | (Math.random() - 0.5) * NBody.Constant.initialVelocity, |
michael@0 | 33 | (Math.random()) * NBody.Constant.initialVelocity]; |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | NBody.private = {}; |
michael@0 | 37 | |
michael@0 | 38 | if (mode === "par") { |
michael@0 | 39 | NBody.private.pos = new ParallelArray(initPos); |
michael@0 | 40 | NBody.private.vel = new ParallelArray(initVel); |
michael@0 | 41 | } else { |
michael@0 | 42 | NBody.private.pos = initPos; |
michael@0 | 43 | NBody.private.vel = initVel; |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | NBody.numBodies = numBodies; |
michael@0 | 47 | NBody.time = 0; |
michael@0 | 48 | }, |
michael@0 | 49 | |
michael@0 | 50 | // Parallel |
michael@0 | 51 | |
michael@0 | 52 | tickPar: function tickPar() { |
michael@0 | 53 | NBody.private.vel = new ParallelArray([NBody.numBodies], NBody.velocityPar); |
michael@0 | 54 | NBody.private.pos = new ParallelArray([NBody.numBodies], NBody.positionPar); |
michael@0 | 55 | NBody.time++; |
michael@0 | 56 | }, |
michael@0 | 57 | |
michael@0 | 58 | velocityPar: function velocityPar(index) { |
michael@0 | 59 | var pos = NBody.private.pos; |
michael@0 | 60 | var vel = NBody.private.vel; |
michael@0 | 61 | |
michael@0 | 62 | var deltaTime = NBody.Constant.deltaTime; |
michael@0 | 63 | var epsSqr = NBody.Constant.epsSqr; |
michael@0 | 64 | var time = NBody.time; |
michael@0 | 65 | |
michael@0 | 66 | var shape = vel.shape[0]; |
michael@0 | 67 | |
michael@0 | 68 | var newVel; |
michael@0 | 69 | var newX, newY, newZ; |
michael@0 | 70 | var newX2, newY2, newZ2; |
michael@0 | 71 | |
michael@0 | 72 | var cX = Math.cos(time / 22) * -4200; |
michael@0 | 73 | var cY = Math.sin(time / 14) * 9200; |
michael@0 | 74 | var cZ = Math.sin(time / 27) * 6000; |
michael@0 | 75 | |
michael@0 | 76 | // pull to center |
michael@0 | 77 | var maxDistance = 3400; |
michael@0 | 78 | var pullStrength = .042; |
michael@0 | 79 | |
michael@0 | 80 | var speedLimit = 8; |
michael@0 | 81 | |
michael@0 | 82 | // zones |
michael@0 | 83 | var zone = 400; |
michael@0 | 84 | var repel = 100; |
michael@0 | 85 | var align = 300; |
michael@0 | 86 | var attract = 100; |
michael@0 | 87 | |
michael@0 | 88 | if (time < 500) { |
michael@0 | 89 | speedLimit = 2000; |
michael@0 | 90 | var attractPower = 100.9; |
michael@0 | 91 | } else { |
michael@0 | 92 | speedLimit = .2; |
michael@0 | 93 | attractPower = 20.9; |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | var zoneSqrd = zone * zone + zone * zone + zone * zone; |
michael@0 | 97 | |
michael@0 | 98 | var accX = 0, accY = 0, accZ = 0; |
michael@0 | 99 | var accX2 = 0, accY2 = 0, accZ2 = 0; |
michael@0 | 100 | var i; |
michael@0 | 101 | |
michael@0 | 102 | // define particle 1 center distance |
michael@0 | 103 | var dirToCenterX = cX - pos.get(index)[0]; |
michael@0 | 104 | var dirToCenterY = cY - pos.get(index)[1]; |
michael@0 | 105 | var dirToCenterZ = cZ - pos.get(index)[2]; |
michael@0 | 106 | |
michael@0 | 107 | var distanceSquaredTo = dirToCenterX * dirToCenterX + dirToCenterY * dirToCenterY + dirToCenterZ * dirToCenterZ; |
michael@0 | 108 | var distToCenter = Math.sqrt(distanceSquaredTo); |
michael@0 | 109 | |
michael@0 | 110 | // orient to center |
michael@0 | 111 | if (distToCenter > maxDistance) { |
michael@0 | 112 | var velc = (distToCenter - maxDistance) * pullStrength; |
michael@0 | 113 | if (time < 200) |
michael@0 | 114 | velc = .2; |
michael@0 | 115 | else velc = (distToCenter - maxDistance) * pullStrength; |
michael@0 | 116 | |
michael@0 | 117 | accX += (dirToCenterX / distToCenter) * velc; |
michael@0 | 118 | accY += (dirToCenterY / distToCenter) * velc; |
michael@0 | 119 | accZ += (dirToCenterZ / distToCenter) * velc; |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | for (i = 0; i < shape; i = i + 1) { |
michael@0 | 123 | var rx = pos.get(i)[0] - pos.get(index)[0]; |
michael@0 | 124 | var ry = pos.get(i)[1] - pos.get(index)[1]; |
michael@0 | 125 | var rz = pos.get(i)[2] - pos.get(index)[2]; |
michael@0 | 126 | |
michael@0 | 127 | // make sure we are not testing the particle against its own position |
michael@0 | 128 | var areSame = 0; |
michael@0 | 129 | if (pos.get(i)[0] == pos.get(index)[0] && pos.get(i)[1] == pos.get(index)[1] && pos.get(i)[2] == pos.get(index)[2]) |
michael@0 | 130 | areSame += 1; |
michael@0 | 131 | |
michael@0 | 132 | var distSqrd = rx * rx + ry * ry + rz * rz; |
michael@0 | 133 | |
michael@0 | 134 | // cant use eqals to test, only <= or >= WTF |
michael@0 | 135 | if (distSqrd < zoneSqrd && areSame <= 0) { |
michael@0 | 136 | var length = Math.sqrt(distSqrd); |
michael@0 | 137 | var percent = distSqrd / zoneSqrd; |
michael@0 | 138 | |
michael@0 | 139 | if (distSqrd < repel) { |
michael@0 | 140 | var F = (repel / percent - 1) * .025; |
michael@0 | 141 | |
michael@0 | 142 | var normalRx = (rx / length) * F; |
michael@0 | 143 | var normalRy = (ry / length) * F; |
michael@0 | 144 | var normalRz = (rz / length) * F; |
michael@0 | 145 | |
michael@0 | 146 | accX = accX + normalRx; |
michael@0 | 147 | accY = accY + normalRy; |
michael@0 | 148 | accZ = accZ + normalRz; |
michael@0 | 149 | |
michael@0 | 150 | accX2 = accX2 - normalRx; |
michael@0 | 151 | accY2 = accY2 - normalRy; |
michael@0 | 152 | accZ2 = accZ2 - normalRz; |
michael@0 | 153 | } else if (distSqrd < align) { //align |
michael@0 | 154 | var threshDelta = align - repel; |
michael@0 | 155 | var adjustedPercent = (percent - repel) / threshDelta; |
michael@0 | 156 | var Q = (.5 - Math.cos(adjustedPercent * 3.14159265 * 2) * .5 + .5) * 100.9; |
michael@0 | 157 | |
michael@0 | 158 | // get velocity 2 |
michael@0 | 159 | var velX2 = vel.get(i)[4]; |
michael@0 | 160 | var velY2 = vel.get(i)[5]; |
michael@0 | 161 | var velZ2 = vel.get(i)[6]; |
michael@0 | 162 | |
michael@0 | 163 | var velLength2 = Math.sqrt(velX2 * velX2 + velY2 * velY2 + velZ2 * velZ2); |
michael@0 | 164 | |
michael@0 | 165 | // normalize vel2 and multiply by factor |
michael@0 | 166 | velX2 = (velX2 / velLength2) * Q; |
michael@0 | 167 | velY2 = (velY2 / velLength2) * Q; |
michael@0 | 168 | velZ2 = (velZ2 / velLength2) * Q; |
michael@0 | 169 | |
michael@0 | 170 | // get own velocity |
michael@0 | 171 | var velX = vel.get(i)[0]; |
michael@0 | 172 | var velY = vel.get(i)[1]; |
michael@0 | 173 | var velZ = vel.get(i)[2]; |
michael@0 | 174 | |
michael@0 | 175 | var velLength = Math.sqrt(velX * velX + velY * velY + velZ * velZ); |
michael@0 | 176 | |
michael@0 | 177 | // normalize own velocity |
michael@0 | 178 | velX = (velX / velLength) * Q; |
michael@0 | 179 | velY = (velY / velLength) * Q; |
michael@0 | 180 | velZ = (velZ / velLength) * Q; |
michael@0 | 181 | |
michael@0 | 182 | accX += velX2; |
michael@0 | 183 | accY += velY2; |
michael@0 | 184 | accZ += velZ2; |
michael@0 | 185 | |
michael@0 | 186 | accX2 += velX; |
michael@0 | 187 | accY2 += velY; |
michael@0 | 188 | accZ2 += velZ; |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | if (distSqrd > attract) { // attract |
michael@0 | 192 | var threshDelta2 = 1 - attract; |
michael@0 | 193 | var adjustedPercent2 = (percent - attract) / threshDelta2; |
michael@0 | 194 | var C = (1 - (Math.cos(adjustedPercent2 * 3.14159265 * 2) * 0.5 + 0.5)) * attractPower; |
michael@0 | 195 | |
michael@0 | 196 | // normalize the distance vector |
michael@0 | 197 | var dx = (rx / (length)) * C; |
michael@0 | 198 | var dy = (ry / (length)) * C; |
michael@0 | 199 | var dz = (rz / (length)) * C; |
michael@0 | 200 | |
michael@0 | 201 | accX += dx; |
michael@0 | 202 | accY += dy; |
michael@0 | 203 | accZ += dz; |
michael@0 | 204 | |
michael@0 | 205 | accX2 -= dx; |
michael@0 | 206 | accY2 -= dy; |
michael@0 | 207 | accZ2 -= dz; |
michael@0 | 208 | } |
michael@0 | 209 | } |
michael@0 | 210 | } |
michael@0 | 211 | |
michael@0 | 212 | // Speed limits |
michael@0 | 213 | if (time > 500) { |
michael@0 | 214 | var accSquared = accX * accX + accY * accY + accZ * accZ; |
michael@0 | 215 | if (accSquared > speedLimit) { |
michael@0 | 216 | accX = accX * .015; |
michael@0 | 217 | accY = accY * .015; |
michael@0 | 218 | accZ = accZ * .015; |
michael@0 | 219 | } |
michael@0 | 220 | |
michael@0 | 221 | var accSquared2 = accX2 * accX2 + accY2 * accY2 + accZ2 * accZ2; |
michael@0 | 222 | if (accSquared2 > speedLimit) { |
michael@0 | 223 | accX2 = accX2 * .015; |
michael@0 | 224 | accY2 = accY2 * .015; |
michael@0 | 225 | accZ2 = accZ2 * .015; |
michael@0 | 226 | } |
michael@0 | 227 | } |
michael@0 | 228 | |
michael@0 | 229 | // Caclulate new velocity |
michael@0 | 230 | newX = (vel.get(index)[0]) + accX; |
michael@0 | 231 | newY = (vel.get(index)[1]) + accY; |
michael@0 | 232 | newZ = (vel.get(index)[2]) + accZ; |
michael@0 | 233 | |
michael@0 | 234 | newX2 = (vel.get(index)[3]) + accX2; |
michael@0 | 235 | newY2 = (vel.get(index)[4]) + accY2; |
michael@0 | 236 | newZ2 = (vel.get(index)[5]) + accZ2; |
michael@0 | 237 | |
michael@0 | 238 | if (time < 500) { |
michael@0 | 239 | var acs = newX2 * newX2 + newY2 * newY2 + newZ2 * newZ2; |
michael@0 | 240 | if (acs > speedLimit) { |
michael@0 | 241 | newX2 = newX2 * .15; |
michael@0 | 242 | newY2 = newY2 * .15; |
michael@0 | 243 | newZ2 = newZ2 * .15; |
michael@0 | 244 | } |
michael@0 | 245 | |
michael@0 | 246 | var acs2 = newX * newX + newY * newY + newZ * newZ; |
michael@0 | 247 | if (acs2 > speedLimit) { |
michael@0 | 248 | newX = newX * .15; |
michael@0 | 249 | newY = newY * .15; |
michael@0 | 250 | newZ = newZ * .15; |
michael@0 | 251 | } |
michael@0 | 252 | } |
michael@0 | 253 | |
michael@0 | 254 | return [newX, newY, newZ, newX2, newY2, newZ2]; |
michael@0 | 255 | }, |
michael@0 | 256 | |
michael@0 | 257 | positionPar: function positionPar(index) { |
michael@0 | 258 | var vel = NBody.private.vel; |
michael@0 | 259 | var pos = NBody.private.pos; |
michael@0 | 260 | |
michael@0 | 261 | var x = 0; |
michael@0 | 262 | var y = 0; |
michael@0 | 263 | var z = 0; |
michael@0 | 264 | |
michael@0 | 265 | var velX = vel.get(index)[0]; |
michael@0 | 266 | var velY = vel.get(index)[1]; |
michael@0 | 267 | var velZ = vel.get(index)[2]; |
michael@0 | 268 | |
michael@0 | 269 | var velX2 = vel.get(index)[3]; |
michael@0 | 270 | var velY2 = vel.get(index)[4]; |
michael@0 | 271 | var velZ2 = vel.get(index)[5]; |
michael@0 | 272 | |
michael@0 | 273 | var netVelX = (velX - velX2); |
michael@0 | 274 | var netVelY = (velY - velY2); |
michael@0 | 275 | var netVelZ = (velZ - velZ2); |
michael@0 | 276 | |
michael@0 | 277 | x = pos.get(index)[0] + (netVelX); |
michael@0 | 278 | y = pos.get(index)[1] + (netVelY); |
michael@0 | 279 | z = pos.get(index)[2] + (netVelZ); |
michael@0 | 280 | |
michael@0 | 281 | return [x, y, z]; |
michael@0 | 282 | }, |
michael@0 | 283 | |
michael@0 | 284 | // Sequential |
michael@0 | 285 | |
michael@0 | 286 | tickSeq: function tickSeq() { |
michael@0 | 287 | var numBodies = NBody.numBodies; |
michael@0 | 288 | var newVel = new Array(numBodies); |
michael@0 | 289 | var newPos = new Array(numBodies); |
michael@0 | 290 | |
michael@0 | 291 | for (var i = 0; i < numBodies; i++) |
michael@0 | 292 | newVel[i] = NBody.velocitySeq(i); |
michael@0 | 293 | |
michael@0 | 294 | for (var i = 0; i < numBodies; i++) |
michael@0 | 295 | newPos[i] = NBody.positionSeq(i); |
michael@0 | 296 | |
michael@0 | 297 | NBody.private.vel = newVel; |
michael@0 | 298 | NBody.private.pos = newPos; |
michael@0 | 299 | |
michael@0 | 300 | NBody.time++; |
michael@0 | 301 | }, |
michael@0 | 302 | |
michael@0 | 303 | velocitySeq: function velocitySeq(index) { |
michael@0 | 304 | var vel = NBody.private.vel; |
michael@0 | 305 | var pos = NBody.private.pos; |
michael@0 | 306 | |
michael@0 | 307 | var deltaTime = NBody.Constant.deltaTime; |
michael@0 | 308 | var epsSqr = NBody.Constant.epsSqr; |
michael@0 | 309 | var time = NBody.time; |
michael@0 | 310 | |
michael@0 | 311 | var shape = pos.length; |
michael@0 | 312 | |
michael@0 | 313 | var newVel; |
michael@0 | 314 | var newX, newY, newZ; |
michael@0 | 315 | var newX2, newY2, newZ2; |
michael@0 | 316 | |
michael@0 | 317 | var cX = Math.cos(time / 22) * -4200; |
michael@0 | 318 | var cY = Math.sin(time / 14) * 9200; |
michael@0 | 319 | var cZ = Math.sin(time / 27) * 6000; |
michael@0 | 320 | |
michael@0 | 321 | // pull to center |
michael@0 | 322 | var maxDistance = 3400; |
michael@0 | 323 | var pullStrength = .042; |
michael@0 | 324 | |
michael@0 | 325 | var speedLimit = 8; |
michael@0 | 326 | |
michael@0 | 327 | // zones |
michael@0 | 328 | var zone = 400; |
michael@0 | 329 | var repel = 100; |
michael@0 | 330 | var align = 300; |
michael@0 | 331 | var attract = 100; |
michael@0 | 332 | |
michael@0 | 333 | |
michael@0 | 334 | if (time < 500) { |
michael@0 | 335 | speedLimit = 2000; |
michael@0 | 336 | var attractPower = 100.9; |
michael@0 | 337 | } else { |
michael@0 | 338 | speedLimit = .2; |
michael@0 | 339 | attractPower = 20.9; |
michael@0 | 340 | } |
michael@0 | 341 | |
michael@0 | 342 | var zoneSqrd = zone * zone + zone * zone + zone * zone; |
michael@0 | 343 | |
michael@0 | 344 | var accX = 0, accY = 0, accZ = 0; |
michael@0 | 345 | var accX2 = 0, accY2 = 0, accZ2 = 0; |
michael@0 | 346 | var i; |
michael@0 | 347 | |
michael@0 | 348 | // define particle 1 center distance |
michael@0 | 349 | var dirToCenterX = cX - pos[index][0]; |
michael@0 | 350 | var dirToCenterY = cY - pos[index][1]; |
michael@0 | 351 | var dirToCenterZ = cZ - pos[index][2]; |
michael@0 | 352 | |
michael@0 | 353 | var distanceSquaredTo = dirToCenterX * dirToCenterX + dirToCenterY * dirToCenterY + dirToCenterZ * dirToCenterZ; |
michael@0 | 354 | var distToCenter = Math.sqrt(distanceSquaredTo); |
michael@0 | 355 | |
michael@0 | 356 | // orient to center |
michael@0 | 357 | if (distToCenter > maxDistance) { |
michael@0 | 358 | var velc = (distToCenter - maxDistance) * pullStrength; |
michael@0 | 359 | if (time < 200) |
michael@0 | 360 | velc = .2; |
michael@0 | 361 | else velc = (distToCenter - maxDistance) * pullStrength; |
michael@0 | 362 | |
michael@0 | 363 | accX += (dirToCenterX / distToCenter) * velc; |
michael@0 | 364 | accY += (dirToCenterY / distToCenter) * velc; |
michael@0 | 365 | accZ += (dirToCenterZ / distToCenter) * velc; |
michael@0 | 366 | } |
michael@0 | 367 | |
michael@0 | 368 | for (i = 0; i < shape; i = i + 1) { |
michael@0 | 369 | var rx = pos[i][0] - pos[index][0]; |
michael@0 | 370 | var ry = pos[i][1] - pos[index][1]; |
michael@0 | 371 | var rz = pos[i][2] - pos[index][2]; |
michael@0 | 372 | |
michael@0 | 373 | var areSame = 0; |
michael@0 | 374 | if (pos[i][0] == pos[index][0] && pos[i][1] == pos[index][1] && pos[i][2] == pos[index][2]) |
michael@0 | 375 | areSame += 1; |
michael@0 | 376 | |
michael@0 | 377 | var distSqrd = rx * rx + ry * ry + rz * rz; |
michael@0 | 378 | |
michael@0 | 379 | // cant use eqals to test, only <= or >= WTF |
michael@0 | 380 | if (distSqrd < zoneSqrd && areSame <= 0) { |
michael@0 | 381 | var length = Math.sqrt(distSqrd); |
michael@0 | 382 | var percent = distSqrd / zoneSqrd; |
michael@0 | 383 | |
michael@0 | 384 | |
michael@0 | 385 | if (distSqrd < repel) { |
michael@0 | 386 | var F = (repel / percent - 1) * .025; |
michael@0 | 387 | |
michael@0 | 388 | var normalRx = (rx / length) * F; |
michael@0 | 389 | var normalRy = (ry / length) * F; |
michael@0 | 390 | var normalRz = (rz / length) * F; |
michael@0 | 391 | |
michael@0 | 392 | accX = accX + normalRx; |
michael@0 | 393 | accY = accY + normalRy; |
michael@0 | 394 | accZ = accZ + normalRz; |
michael@0 | 395 | |
michael@0 | 396 | accX2 = accX2 - normalRx; |
michael@0 | 397 | accY2 = accY2 - normalRy; |
michael@0 | 398 | accZ2 = accZ2 - normalRz; |
michael@0 | 399 | } else if (distSqrd < align) { //align |
michael@0 | 400 | var threshDelta = align - repel; |
michael@0 | 401 | var adjustedPercent = (percent - repel) / threshDelta; |
michael@0 | 402 | var Q = (.5 - Math.cos(adjustedPercent * 3.14159265 * 2) * .5 + .5) * 100; |
michael@0 | 403 | |
michael@0 | 404 | // get velocity 2 |
michael@0 | 405 | var velX2 = vel[i][3]; |
michael@0 | 406 | var velY2 = vel[i][4]; |
michael@0 | 407 | var velZ2 = vel[i][5]; |
michael@0 | 408 | |
michael@0 | 409 | var velLength2 = Math.sqrt(velX2 * velX2 + velY2 * velY2 + velZ2 * velZ2); |
michael@0 | 410 | |
michael@0 | 411 | // normalize vel2 and multiply by factor |
michael@0 | 412 | velX2 = (velX2 / velLength2) * Q; |
michael@0 | 413 | velY2 = (velY2 / velLength2) * Q; |
michael@0 | 414 | velZ2 = (velZ2 / velLength2) * Q; |
michael@0 | 415 | |
michael@0 | 416 | // get own velocity |
michael@0 | 417 | var velX = vel[i][0]; |
michael@0 | 418 | var velY = vel[i][1]; |
michael@0 | 419 | var velZ = vel[i][2]; |
michael@0 | 420 | |
michael@0 | 421 | var velLength = Math.sqrt(velX * velX + velY * velY + velZ * velZ); |
michael@0 | 422 | |
michael@0 | 423 | // normalize own velocity |
michael@0 | 424 | velX = (velX / velLength) * Q; |
michael@0 | 425 | velY = (velY / velLength) * Q; |
michael@0 | 426 | velZ = (velZ / velLength) * Q; |
michael@0 | 427 | |
michael@0 | 428 | accX += velX2; |
michael@0 | 429 | accY += velY2; |
michael@0 | 430 | accZ += velZ2; |
michael@0 | 431 | |
michael@0 | 432 | accX2 += velX; |
michael@0 | 433 | accY2 += velY; |
michael@0 | 434 | accZ2 += velZ; |
michael@0 | 435 | } |
michael@0 | 436 | |
michael@0 | 437 | if (distSqrd > attract) { //attract |
michael@0 | 438 | var threshDelta2 = 1 - align; |
michael@0 | 439 | var adjustedPercent2 = (percent - align) / threshDelta2; |
michael@0 | 440 | var C = (1 - (Math.cos(adjustedPercent2 * 3.14159265 * 2) * 0.5 + 0.5)) * attractPower; |
michael@0 | 441 | |
michael@0 | 442 | // normalize the distance vector |
michael@0 | 443 | var dx = (rx / (length)) * C; |
michael@0 | 444 | var dy = (ry / (length)) * C; |
michael@0 | 445 | var dz = (rz / (length)) * C; |
michael@0 | 446 | |
michael@0 | 447 | debug = 1.1; |
michael@0 | 448 | |
michael@0 | 449 | accX += dx; |
michael@0 | 450 | accY += dy; |
michael@0 | 451 | accZ += dz; |
michael@0 | 452 | |
michael@0 | 453 | accX2 -= dx; |
michael@0 | 454 | accY2 -= dy; |
michael@0 | 455 | accZ2 -= dz; |
michael@0 | 456 | } |
michael@0 | 457 | } |
michael@0 | 458 | } |
michael@0 | 459 | |
michael@0 | 460 | // enforce speed limits |
michael@0 | 461 | if (time > 500) { |
michael@0 | 462 | var accSquared = accX * accX + accY * accY + accZ * accZ; |
michael@0 | 463 | if (accSquared > speedLimit) { |
michael@0 | 464 | accX = accX * .015; |
michael@0 | 465 | accY = accY * .015; |
michael@0 | 466 | accZ = accZ * .015; |
michael@0 | 467 | } |
michael@0 | 468 | |
michael@0 | 469 | var accSquared2 = accX2 * accX2 + accY2 * accY2 + accZ2 * accZ2; |
michael@0 | 470 | if (accSquared2 > speedLimit) { |
michael@0 | 471 | accX2 = accX2 * .015; |
michael@0 | 472 | accY2 = accY2 * .015; |
michael@0 | 473 | accZ2 = accZ2 * .015; |
michael@0 | 474 | } |
michael@0 | 475 | } |
michael@0 | 476 | |
michael@0 | 477 | // Caclulate new velocity |
michael@0 | 478 | newX = vel[index][0] + accX; |
michael@0 | 479 | newY = vel[index][1] + accY; |
michael@0 | 480 | newZ = vel[index][2] + accZ; |
michael@0 | 481 | |
michael@0 | 482 | newX2 = vel[index][3] + accX2; |
michael@0 | 483 | newY2 = vel[index][4] + accY2; |
michael@0 | 484 | newZ2 = vel[index][5] + accZ2; |
michael@0 | 485 | |
michael@0 | 486 | if (time < 500) { |
michael@0 | 487 | var acs = newX2 * newX2 + newY2 * newY2 + newZ2 * newZ2; |
michael@0 | 488 | if (acs > speedLimit) { |
michael@0 | 489 | newX2 = newX2 * .15; |
michael@0 | 490 | newY2 = newY2 * .15; |
michael@0 | 491 | newZ2 = newZ2 * .15; |
michael@0 | 492 | } |
michael@0 | 493 | |
michael@0 | 494 | var acs2 = newX * newX + newY * newY + newZ * newZ; |
michael@0 | 495 | if (acs2 > speedLimit) { |
michael@0 | 496 | newX = newX * .15; |
michael@0 | 497 | newY = newY * .15; |
michael@0 | 498 | newZ = newZ * .15; |
michael@0 | 499 | } |
michael@0 | 500 | } |
michael@0 | 501 | |
michael@0 | 502 | return [newX, newY, newZ, newX2, newY2, newZ2]; |
michael@0 | 503 | }, |
michael@0 | 504 | |
michael@0 | 505 | positionSeq: function positionSeq(index) { |
michael@0 | 506 | var vel = NBody.private.vel; |
michael@0 | 507 | var pos = NBody.private.pos; |
michael@0 | 508 | |
michael@0 | 509 | var x = 0; |
michael@0 | 510 | var y = 0; |
michael@0 | 511 | var z = 0; |
michael@0 | 512 | |
michael@0 | 513 | var velX = vel[index][0]; |
michael@0 | 514 | var velY = vel[index][1]; |
michael@0 | 515 | var velZ = vel[index][2]; |
michael@0 | 516 | |
michael@0 | 517 | var velX2 = vel[index][3]; |
michael@0 | 518 | var velY2 = vel[index][4]; |
michael@0 | 519 | var velZ2 = vel[index][5]; |
michael@0 | 520 | |
michael@0 | 521 | var netX = (velX - velX2); |
michael@0 | 522 | var netY = (velY - velY2); |
michael@0 | 523 | var netZ = (velZ - velZ2); |
michael@0 | 524 | |
michael@0 | 525 | x = pos[index][0] + netX; |
michael@0 | 526 | y = pos[index][1] + netY; |
michael@0 | 527 | z = pos[index][2] + netZ; |
michael@0 | 528 | |
michael@0 | 529 | return [x, y, z]; |
michael@0 | 530 | } |
michael@0 | 531 | }; |
michael@0 | 532 | |
michael@0 | 533 | function emulateNBody(mode, numBodies, ticks) { |
michael@0 | 534 | NBody.init(mode, numBodies); |
michael@0 | 535 | for (var i = 0; i < ticks; i++) { |
michael@0 | 536 | var start = Date.now(); |
michael@0 | 537 | if (mode === "par") |
michael@0 | 538 | NBody.tickPar(); |
michael@0 | 539 | else |
michael@0 | 540 | NBody.tickSeq(); |
michael@0 | 541 | //print(NBody.private.pos); |
michael@0 | 542 | print(mode + " bodies=" + numBodies + " tick=" + (i+1) + "/" + ticks + ": " + (Date.now() - start) + " ms"); |
michael@0 | 543 | } |
michael@0 | 544 | } |
michael@0 | 545 | |
michael@0 | 546 | // Using 4000 bodies going off Rick's comment as 4000 being a typical workload. |
michael@0 | 547 | const NUMBODIES = 4000; |
michael@0 | 548 | const TICKS = 10; |
michael@0 | 549 | |
michael@0 | 550 | Math.seedrandom("seed"); |
michael@0 | 551 | |
michael@0 | 552 | benchmark("NBODY", 1, DEFAULT_MEASURE, |
michael@0 | 553 | function () { emulateNBody("seq", NUMBODIES, TICKS); }, |
michael@0 | 554 | function () { emulateNBody("par", NUMBODIES, TICKS); }); |