2012-12-10 19:32:48 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2011-03-24 20:20:49 -05:00
|
|
|
// based on:
|
|
|
|
// http://shootout.alioth.debian.org/u32/benchmark.php?test=nbody&lang=java
|
|
|
|
|
2012-09-11 19:46:20 -05:00
|
|
|
extern mod std;
|
2012-01-14 22:26:44 -06:00
|
|
|
|
2012-12-28 21:57:18 -06:00
|
|
|
use core::os;
|
|
|
|
|
2012-01-14 22:26:44 -06:00
|
|
|
// Using sqrt from the standard library is way slower than using libc
|
|
|
|
// directly even though std just calls libc, I guess it must be
|
|
|
|
// because the the indirection through another dynamic linker
|
|
|
|
// stub. Kind of shocking. Might be able to make it faster still with
|
|
|
|
// an llvm intrinsic.
|
2011-12-15 14:25:29 -06:00
|
|
|
#[nolink]
|
2012-07-03 18:11:00 -05:00
|
|
|
extern mod libc {
|
2012-09-21 20:10:45 -05:00
|
|
|
#[legacy_exports];
|
2011-10-24 14:33:08 -05:00
|
|
|
fn sqrt(n: float) -> float;
|
2011-03-28 10:23:33 -05:00
|
|
|
}
|
|
|
|
|
2012-10-03 21:16:27 -05:00
|
|
|
fn main() {
|
|
|
|
let args = os::args();
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = if os::getenv(~"RUST_BENCH").is_some() {
|
|
|
|
~[~"", ~"4000000"]
|
2012-05-24 00:53:50 -05:00
|
|
|
} else if args.len() <= 1u {
|
2012-07-14 00:57:48 -05:00
|
|
|
~[~"", ~"100000"]
|
2012-01-14 22:26:44 -06:00
|
|
|
} else {
|
2012-05-24 00:53:50 -05:00
|
|
|
args
|
2012-01-14 22:26:44 -06:00
|
|
|
};
|
2012-05-24 00:53:50 -05:00
|
|
|
let n = int::from_str(args[1]).get();
|
2012-12-07 14:21:30 -06:00
|
|
|
let mut bodies: ~[Body::props] = NBodySystem::make();
|
|
|
|
io::println(fmt!("%f", NBodySystem::energy(bodies)));
|
2012-07-02 15:50:49 -05:00
|
|
|
let mut i = 0;
|
2012-12-07 14:21:30 -06:00
|
|
|
while i < n {
|
|
|
|
NBodySystem::advance(bodies, 0.01);
|
|
|
|
i += 1;
|
|
|
|
}
|
2012-08-22 19:24:52 -05:00
|
|
|
io::println(fmt!("%f", NBodySystem::energy(bodies)));
|
2011-03-24 20:20:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
mod NBodySystem {
|
2012-12-28 21:57:18 -06:00
|
|
|
use Body;
|
2011-03-24 20:20:49 -05:00
|
|
|
|
2012-12-28 21:57:18 -06:00
|
|
|
pub fn make() -> ~[Body::props] {
|
2012-12-07 14:21:30 -06:00
|
|
|
let mut bodies: ~[Body::props] =
|
|
|
|
~[Body::sun(),
|
|
|
|
Body::jupiter(),
|
|
|
|
Body::saturn(),
|
|
|
|
Body::uranus(),
|
2012-07-02 15:50:49 -05:00
|
|
|
Body::neptune()];
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2012-07-02 15:50:49 -05:00
|
|
|
let mut px = 0.0;
|
|
|
|
let mut py = 0.0;
|
|
|
|
let mut pz = 0.0;
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2012-07-02 15:50:49 -05:00
|
|
|
let mut i = 0;
|
2011-07-27 07:19:39 -05:00
|
|
|
while i < 5 {
|
2011-08-19 17:16:48 -05:00
|
|
|
px += bodies[i].vx * bodies[i].mass;
|
|
|
|
py += bodies[i].vy * bodies[i].mass;
|
|
|
|
pz += bodies[i].vz * bodies[i].mass;
|
2011-03-26 17:14:22 -05:00
|
|
|
|
|
|
|
i += 1;
|
2011-03-24 20:20:49 -05:00
|
|
|
}
|
2011-03-25 15:40:40 -05:00
|
|
|
|
|
|
|
// side-effecting
|
2012-12-07 14:21:30 -06:00
|
|
|
Body::offset_momentum(&mut bodies[0], px, py, pz);
|
2011-03-24 20:20:49 -05:00
|
|
|
|
2012-08-01 19:30:05 -05:00
|
|
|
return bodies;
|
2011-03-24 20:20:49 -05:00
|
|
|
}
|
|
|
|
|
2012-12-28 21:57:18 -06:00
|
|
|
pub fn advance(bodies: &mut [Body::props], dt: float) {
|
2012-07-02 15:50:49 -05:00
|
|
|
let mut i = 0;
|
2011-07-27 07:19:39 -05:00
|
|
|
while i < 5 {
|
2012-07-02 15:50:49 -05:00
|
|
|
let mut j = i + 1;
|
2012-12-07 14:21:30 -06:00
|
|
|
while j < 5 {
|
|
|
|
advance_one(&mut bodies[i],
|
|
|
|
&mut bodies[j], dt);
|
|
|
|
j += 1;
|
|
|
|
}
|
2011-03-24 20:20:49 -05:00
|
|
|
|
2011-03-26 17:14:22 -05:00
|
|
|
i += 1;
|
2011-03-24 20:20:49 -05:00
|
|
|
}
|
|
|
|
|
2011-03-26 17:14:22 -05:00
|
|
|
i = 0;
|
2012-12-07 14:21:30 -06:00
|
|
|
while i < 5 {
|
|
|
|
move_(&mut bodies[i], dt);
|
|
|
|
i += 1;
|
|
|
|
}
|
2011-03-24 20:20:49 -05:00
|
|
|
}
|
|
|
|
|
2012-12-28 21:57:18 -06:00
|
|
|
pub fn advance_one(bi: &mut Body::props,
|
|
|
|
bj: &mut Body::props,
|
2013-01-23 20:02:15 -06:00
|
|
|
dt: float) {
|
|
|
|
unsafe {
|
|
|
|
let dx = bi.x - bj.x;
|
|
|
|
let dy = bi.y - bj.y;
|
|
|
|
let dz = bi.z - bj.z;
|
2011-03-27 01:06:06 -05:00
|
|
|
|
2013-01-23 20:02:15 -06:00
|
|
|
let dSquared = dx * dx + dy * dy + dz * dz;
|
2011-03-27 01:06:06 -05:00
|
|
|
|
2013-01-23 20:02:15 -06:00
|
|
|
let distance = ::libc::sqrt(dSquared);
|
|
|
|
let mag = dt / (dSquared * distance);
|
2011-03-27 01:06:06 -05:00
|
|
|
|
2013-01-23 20:02:15 -06:00
|
|
|
bi.vx -= dx * bj.mass * mag;
|
|
|
|
bi.vy -= dy * bj.mass * mag;
|
|
|
|
bi.vz -= dz * bj.mass * mag;
|
2011-03-27 01:06:06 -05:00
|
|
|
|
2013-01-23 20:02:15 -06:00
|
|
|
bj.vx += dx * bi.mass * mag;
|
|
|
|
bj.vy += dy * bi.mass * mag;
|
|
|
|
bj.vz += dz * bi.mass * mag;
|
|
|
|
}
|
2011-03-27 01:06:06 -05:00
|
|
|
}
|
|
|
|
|
2012-12-28 21:57:18 -06:00
|
|
|
pub fn move_(b: &mut Body::props, dt: float) {
|
2011-03-28 10:23:33 -05:00
|
|
|
b.x += dt * b.vx;
|
|
|
|
b.y += dt * b.vy;
|
|
|
|
b.z += dt * b.vz;
|
|
|
|
}
|
|
|
|
|
2013-01-23 20:02:15 -06:00
|
|
|
pub fn energy(bodies: &[Body::props]) -> float {
|
|
|
|
unsafe {
|
|
|
|
let mut dx;
|
|
|
|
let mut dy;
|
|
|
|
let mut dz;
|
|
|
|
let mut distance;
|
|
|
|
let mut e = 0.0;
|
|
|
|
|
|
|
|
let mut i = 0;
|
|
|
|
while i < 5 {
|
|
|
|
e +=
|
|
|
|
0.5 * bodies[i].mass *
|
|
|
|
(bodies[i].vx * bodies[i].vx
|
|
|
|
+ bodies[i].vy * bodies[i].vy
|
|
|
|
+ bodies[i].vz * bodies[i].vz);
|
|
|
|
|
|
|
|
let mut j = i + 1;
|
|
|
|
while j < 5 {
|
|
|
|
dx = bodies[i].x - bodies[j].x;
|
|
|
|
dy = bodies[i].y - bodies[j].y;
|
|
|
|
dz = bodies[i].z - bodies[j].z;
|
|
|
|
|
|
|
|
distance = ::libc::sqrt(dx * dx
|
|
|
|
+ dy * dy
|
|
|
|
+ dz * dz);
|
|
|
|
e -= bodies[i].mass
|
|
|
|
* bodies[j].mass / distance;
|
|
|
|
|
|
|
|
j += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
i += 1;
|
2011-03-24 20:20:49 -05:00
|
|
|
}
|
2013-01-23 20:02:15 -06:00
|
|
|
return e;
|
2011-03-24 20:20:49 -05:00
|
|
|
}
|
2011-03-26 17:14:22 -05:00
|
|
|
}
|
2011-03-24 20:20:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
mod Body {
|
2012-12-28 21:57:18 -06:00
|
|
|
use Body;
|
2011-03-26 17:14:22 -05:00
|
|
|
|
2012-12-28 21:57:18 -06:00
|
|
|
pub const PI: float = 3.141592653589793;
|
|
|
|
pub const SOLAR_MASS: float = 39.478417604357432;
|
2011-08-19 17:16:48 -05:00
|
|
|
// was 4 * PI * PI originally
|
2012-12-28 21:57:18 -06:00
|
|
|
pub const DAYS_PER_YEAR: float = 365.24;
|
2011-03-24 20:20:49 -05:00
|
|
|
|
2012-12-28 21:57:18 -06:00
|
|
|
pub type props =
|
2012-03-26 20:35:18 -05:00
|
|
|
{mut x: float,
|
|
|
|
mut y: float,
|
|
|
|
mut z: float,
|
|
|
|
mut vx: float,
|
|
|
|
mut vy: float,
|
|
|
|
mut vz: float,
|
2011-07-27 07:19:39 -05:00
|
|
|
mass: float};
|
2011-03-24 20:20:49 -05:00
|
|
|
|
2012-12-28 21:57:18 -06:00
|
|
|
pub fn jupiter() -> Body::props {
|
2012-08-01 19:30:05 -05:00
|
|
|
return {mut x: 4.84143144246472090e+00,
|
2012-03-26 20:35:18 -05:00
|
|
|
mut y: -1.16032004402742839e+00,
|
|
|
|
mut z: -1.03622044471123109e-01,
|
|
|
|
mut vx: 1.66007664274403694e-03 * DAYS_PER_YEAR,
|
|
|
|
mut vy: 7.69901118419740425e-03 * DAYS_PER_YEAR,
|
|
|
|
mut vz: -6.90460016972063023e-05 * DAYS_PER_YEAR,
|
2011-07-27 07:19:39 -05:00
|
|
|
mass: 9.54791938424326609e-04 * SOLAR_MASS};
|
2011-03-24 20:20:49 -05:00
|
|
|
}
|
|
|
|
|
2012-12-28 21:57:18 -06:00
|
|
|
pub fn saturn() -> Body::props {
|
2012-08-01 19:30:05 -05:00
|
|
|
return {mut x: 8.34336671824457987e+00,
|
2012-03-26 20:35:18 -05:00
|
|
|
mut y: 4.12479856412430479e+00,
|
|
|
|
mut z: -4.03523417114321381e-01,
|
|
|
|
mut vx: -2.76742510726862411e-03 * DAYS_PER_YEAR,
|
|
|
|
mut vy: 4.99852801234917238e-03 * DAYS_PER_YEAR,
|
|
|
|
mut vz: 2.30417297573763929e-05 * DAYS_PER_YEAR,
|
2011-07-27 07:19:39 -05:00
|
|
|
mass: 2.85885980666130812e-04 * SOLAR_MASS};
|
|
|
|
}
|
2011-03-24 20:20:49 -05:00
|
|
|
|
2012-12-28 21:57:18 -06:00
|
|
|
pub fn uranus() -> Body::props {
|
2012-08-01 19:30:05 -05:00
|
|
|
return {mut x: 1.28943695621391310e+01,
|
2012-03-26 20:35:18 -05:00
|
|
|
mut y: -1.51111514016986312e+01,
|
|
|
|
mut z: -2.23307578892655734e-01,
|
|
|
|
mut vx: 2.96460137564761618e-03 * DAYS_PER_YEAR,
|
|
|
|
mut vy: 2.37847173959480950e-03 * DAYS_PER_YEAR,
|
|
|
|
mut vz: -2.96589568540237556e-05 * DAYS_PER_YEAR,
|
2011-07-27 07:19:39 -05:00
|
|
|
mass: 4.36624404335156298e-05 * SOLAR_MASS};
|
2011-03-24 20:20:49 -05:00
|
|
|
}
|
|
|
|
|
2012-12-28 21:57:18 -06:00
|
|
|
pub fn neptune() -> Body::props {
|
2012-08-01 19:30:05 -05:00
|
|
|
return {mut x: 1.53796971148509165e+01,
|
2012-03-26 20:35:18 -05:00
|
|
|
mut y: -2.59193146099879641e+01,
|
|
|
|
mut z: 1.79258772950371181e-01,
|
|
|
|
mut vx: 2.68067772490389322e-03 * DAYS_PER_YEAR,
|
|
|
|
mut vy: 1.62824170038242295e-03 * DAYS_PER_YEAR,
|
|
|
|
mut vz: -9.51592254519715870e-05 * DAYS_PER_YEAR,
|
2011-07-27 07:19:39 -05:00
|
|
|
mass: 5.15138902046611451e-05 * SOLAR_MASS};
|
|
|
|
}
|
2011-03-24 20:20:49 -05:00
|
|
|
|
2012-12-28 21:57:18 -06:00
|
|
|
pub fn sun() -> Body::props {
|
2012-08-01 19:30:05 -05:00
|
|
|
return {mut x: 0.0,
|
2012-03-26 20:35:18 -05:00
|
|
|
mut y: 0.0,
|
|
|
|
mut z: 0.0,
|
|
|
|
mut vx: 0.0,
|
|
|
|
mut vy: 0.0,
|
|
|
|
mut vz: 0.0,
|
2011-07-27 07:19:39 -05:00
|
|
|
mass: SOLAR_MASS};
|
|
|
|
}
|
|
|
|
|
2012-12-28 21:57:18 -06:00
|
|
|
pub fn offset_momentum(props: &mut Body::props,
|
2012-12-07 14:21:30 -06:00
|
|
|
px: float, py: float, pz: float) {
|
2011-07-27 07:19:39 -05:00
|
|
|
props.vx = -px / SOLAR_MASS;
|
|
|
|
props.vy = -py / SOLAR_MASS;
|
|
|
|
props.vz = -pz / SOLAR_MASS;
|
|
|
|
}
|
|
|
|
|
2011-08-15 23:54:52 -05:00
|
|
|
}
|