Replace raw pointer parameters with &mut references

This commit is contained in:
Timothy Warren 2020-01-13 10:26:49 -05:00
parent 68e0f20e54
commit fa6f6a7829

View File

@ -92,25 +92,25 @@ static mut solar_Bodies: [body; BODIES_COUNT] = [
// Calculate the momentum of each body and conserve momentum of the system by
// adding to the Sun's velocity the appropriate opposite velocity needed in
// order to offset that body's momentum.
unsafe fn offset_Momentum(bodies: *mut body) {
fn offset_Momentum(bodies: &mut [body; BODIES_COUNT]) {
for i in 0..BODIES_COUNT {
for m in 0..3 {
(*bodies.add(0)).velocity[m] -=
(*bodies.add(i)).velocity[m] * (*bodies.add(i)).mass / SOLAR_MASS;
bodies[0].velocity[m] -=
bodies[i].velocity[m] * bodies[i].mass / SOLAR_MASS;
}
}
}
// Output the total energy of the system.
unsafe fn output_Energy(bodies: *mut body) {
unsafe fn output_Energy(bodies: &mut [body; BODIES_COUNT]) {
let mut energy = 0.;
for i in 0..BODIES_COUNT {
// Add the kinetic energy for each body.
energy += 0.5
* (*bodies.add(i)).mass
* ((*bodies.add(i)).velocity[0] * (*bodies.add(i)).velocity[0]
+ (*bodies.add(i)).velocity[1] * (*bodies.add(i)).velocity[1]
+ (*bodies.add(i)).velocity[2] * (*bodies.add(i)).velocity[2]);
* bodies[i].mass
* (bodies[i].velocity[0] * bodies[i].velocity[0]
+ bodies[i].velocity[1] * bodies[i].velocity[1]
+ bodies[i].velocity[2] * bodies[i].velocity[2]);
// Add the potential energy between this body and
// every other body
@ -120,11 +120,11 @@ unsafe fn output_Energy(bodies: *mut body) {
for m in 0..3 {
position_Delta[m]
.as_mut_ptr()
.write((*bodies.add(i)).position[m] - (*bodies.add(j)).position[m]);
.write(bodies[i].position[m] - bodies[j].position[m]);
}
let position_Delta: [f64; 3] = mem::transmute(position_Delta);
energy -= (*bodies.add(i)).mass * (*bodies.add(j)).mass
energy -= bodies[i].mass * bodies[j].mass
/ f64::sqrt(
position_Delta[0] * position_Delta[0]
+ position_Delta[1] * position_Delta[1]
@ -141,7 +141,7 @@ unsafe fn output_Energy(bodies: *mut body) {
// interactions between all the bodies, update each body's velocity based on
// those interactions, and update each body's position by the distance it
// travels in a timestep at it's updated velocity.
unsafe fn advance(bodies: *mut body) {
unsafe fn advance(bodies: &mut [body; BODIES_COUNT]) {
// Figure out how many total different interactions there are between each
// body and every other body. Some of the calculations for these
// interactions will be calculated two at a time by using x86 SSE
@ -176,7 +176,7 @@ unsafe fn advance(bodies: *mut body) {
for j in i + 1..BODIES_COUNT {
for m in 0..3 {
position_Deltas[m].0[k] =
(*bodies.add(i)).position[m] - (*bodies.add(j)).position[m];
bodies[i].position[m] - bodies[j].position[m];
}
k += 1;
}
@ -254,11 +254,11 @@ unsafe fn advance(bodies: *mut body) {
let mut k = 0;
for i in 0..BODIES_COUNT - 1 {
for j in i + 1..BODIES_COUNT {
let i_mass_magnitude = (*bodies.add(i)).mass * magnitudes.0[k];
let j_mass_magnitude = (*bodies.add(j)).mass * magnitudes.0[k];
let i_mass_magnitude = bodies[i].mass * magnitudes.0[k];
let j_mass_magnitude = bodies[j].mass * magnitudes.0[k];
for m in 0..3 {
(*bodies.add(i)).velocity[m] -= position_Deltas[m].0[k] * j_mass_magnitude;
(*bodies.add(j)).velocity[m] += position_Deltas[m].0[k] * i_mass_magnitude;
bodies[i].velocity[m] -= position_Deltas[m].0[k] * j_mass_magnitude;
bodies[j].velocity[m] += position_Deltas[m].0[k] * i_mass_magnitude;
}
k += 1;
}
@ -268,19 +268,19 @@ unsafe fn advance(bodies: *mut body) {
// Use the updated velocities to update the positions for all of the bodies.
for i in 0..BODIES_COUNT {
for m in 0..3 {
(*bodies.add(i)).position[m] += 0.01 * (*bodies.add(i)).velocity[m];
bodies[i].position[m] += 0.01 * bodies[i].velocity[m];
}
}
}
fn main() {
unsafe {
offset_Momentum(solar_Bodies.as_mut_ptr());
output_Energy(solar_Bodies.as_mut_ptr());
offset_Momentum(&mut solar_Bodies);
output_Energy(&mut solar_Bodies);
let c = std::env::args().nth(1).unwrap().parse().unwrap();
for _ in 0..c {
advance(solar_Bodies.as_mut_ptr());
advance(&mut solar_Bodies);
}
output_Energy(solar_Bodies.as_mut_ptr());
output_Energy(&mut solar_Bodies);
}
}