Update nbody benchmark to more idiomatic Rust; nix obsolete comments
This commit is contained in:
parent
eb28b768af
commit
7a4ebcefcb
@ -22,29 +22,25 @@ fn main(args: ~[str]) {
|
||||
args
|
||||
};
|
||||
let n = int::from_str(args[1]).get();
|
||||
let bodies: ~[Body::props] = NBodySystem::MakeNBodySystem();
|
||||
let bodies: ~[Body::props] = NBodySystem::make();
|
||||
io::println(#fmt("%f", NBodySystem::energy(bodies)));
|
||||
let mut i: int = 0;
|
||||
let mut i = 0;
|
||||
while i < n { NBodySystem::advance(bodies, 0.01); i += 1; }
|
||||
io::println(#fmt("%f", NBodySystem::energy(bodies)));
|
||||
}
|
||||
|
||||
// Body::props is a record of floats, so
|
||||
// vec<Body::props> is a vector of records of floats
|
||||
|
||||
mod NBodySystem {
|
||||
|
||||
fn MakeNBodySystem() -> ~[Body::props] {
|
||||
// these each return a Body::props
|
||||
fn make() -> ~[Body::props] {
|
||||
let bodies: ~[Body::props] =
|
||||
~[Body::sun(), Body::jupiter(), Body::saturn(), Body::uranus(),
|
||||
Body::neptune()];
|
||||
Body::neptune()];
|
||||
|
||||
let mut px: float = 0.0;
|
||||
let mut py: float = 0.0;
|
||||
let mut pz: float = 0.0;
|
||||
let mut px = 0.0;
|
||||
let mut py = 0.0;
|
||||
let mut pz = 0.0;
|
||||
|
||||
let mut i: int = 0;
|
||||
let mut i = 0;
|
||||
while i < 5 {
|
||||
px += bodies[i].vx * bodies[i].mass;
|
||||
py += bodies[i].vy * bodies[i].mass;
|
||||
@ -54,16 +50,16 @@ mod NBodySystem {
|
||||
}
|
||||
|
||||
// side-effecting
|
||||
Body::offsetMomentum(bodies[0], px, py, pz);
|
||||
Body::offset_momentum(bodies[0], px, py, pz);
|
||||
|
||||
ret bodies;
|
||||
}
|
||||
|
||||
fn advance(bodies: ~[Body::props], dt: float) {
|
||||
|
||||
let mut i: int = 0;
|
||||
let mut i = 0;
|
||||
while i < 5 {
|
||||
let mut j: int = i + 1;
|
||||
let mut j = i + 1;
|
||||
while j < 5 { advance_one(bodies[i], bodies[j], dt); j += 1; }
|
||||
|
||||
i += 1;
|
||||
@ -74,14 +70,14 @@ mod NBodySystem {
|
||||
}
|
||||
|
||||
fn advance_one(bi: Body::props, bj: Body::props, dt: float) unsafe {
|
||||
let dx: float = bi.x - bj.x;
|
||||
let dy: float = bi.y - bj.y;
|
||||
let dz: float = bi.z - bj.z;
|
||||
let dx = bi.x - bj.x;
|
||||
let dy = bi.y - bj.y;
|
||||
let dz = bi.z - bj.z;
|
||||
|
||||
let dSquared: float = dx * dx + dy * dy + dz * dz;
|
||||
let dSquared = dx * dx + dy * dy + dz * dz;
|
||||
|
||||
let distance: float = libc::sqrt(dSquared);
|
||||
let mag: float = dt / (dSquared * distance);
|
||||
let distance = libc::sqrt(dSquared);
|
||||
let mag = dt / (dSquared * distance);
|
||||
|
||||
bi.vx -= dx * bj.mass * mag;
|
||||
bi.vy -= dy * bj.mass * mag;
|
||||
@ -99,20 +95,20 @@ mod NBodySystem {
|
||||
}
|
||||
|
||||
fn energy(bodies: ~[Body::props]) -> float unsafe {
|
||||
let mut dx: float;
|
||||
let mut dy: float;
|
||||
let mut dz: float;
|
||||
let mut distance: float;
|
||||
let mut e: float = 0.0;
|
||||
let mut dx;
|
||||
let mut dy;
|
||||
let mut dz;
|
||||
let mut distance;
|
||||
let mut e = 0.0;
|
||||
|
||||
let mut i: int = 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);
|
||||
(bodies[i].vx * bodies[i].vx + bodies[i].vy * bodies[i].vy
|
||||
+ bodies[i].vz * bodies[i].vz);
|
||||
|
||||
let mut j: int = i + 1;
|
||||
let mut j = i + 1;
|
||||
while j < 5 {
|
||||
dx = bodies[i].x - bodies[j].x;
|
||||
dy = bodies[i].y - bodies[j].y;
|
||||
@ -197,7 +193,7 @@ mod Body {
|
||||
mass: SOLAR_MASS};
|
||||
}
|
||||
|
||||
fn offsetMomentum(props: Body::props, px: float, py: float, pz: float) {
|
||||
fn offset_momentum(props: Body::props, px: float, py: float, pz: float) {
|
||||
props.vx = -px / SOLAR_MASS;
|
||||
props.vy = -py / SOLAR_MASS;
|
||||
props.vz = -pz / SOLAR_MASS;
|
||||
|
Loading…
Reference in New Issue
Block a user