Compare commits

..

No commits in common. "6cb8d8e45074b940c60ad15871bc71e485113bd7" and "be6229263dcedfab94972cc01ba459cb7734981c" have entirely different histories.

5 changed files with 11 additions and 53 deletions

View File

@ -3,11 +3,11 @@ use std::error::Error;
use std::fs::File; use std::fs::File;
use std::io::BufReader; use std::io::BufReader;
use std::path::Path; use std::path::Path;
use std::time::{Duration,Instant}; use std::time::Duration;
// External crates // External crates
use clap::Parser; use clap::Parser;
use log::{info, debug}; use log::{info, warn, error, debug, trace};
use indicatif::{ProgressBar, ProgressStyle}; use indicatif::{ProgressBar, ProgressStyle};
use humantime; use humantime;
@ -18,7 +18,7 @@ mod simulator;
// Specific uses from modules // Specific uses from modules
use crate::simulator::Simulation; use crate::simulator::Simulation;
use crate::types::{norm_time, real_body}; use crate::types::{norm_mass, norm_pos, norm_vel, norm_time, real_pos, real_vel};
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
@ -60,27 +60,6 @@ fn read_config<P: AsRef<Path>>(path: P) -> Result<config::ConfigFile, Box<dyn Er
} }
//fn parse_time(arg: &str) //fn parse_time(arg: &str)
fn format_duration_single_unit(dur: Duration) -> String {
let secs = dur.as_secs_f64();
const MINUTE: f64 = 60.0;
const HOUR: f64 = 60.0 * MINUTE;
const DAY: f64 = 24.0 * HOUR;
const YEAR: f64 = 365.25 * DAY;
if secs >= YEAR {
format!("{:.0} years", (secs / YEAR).round())
} else if secs >= DAY {
format!("{:.0} days", (secs / DAY).round())
} else if secs >= HOUR {
format!("{:.0} hours", (secs / HOUR).round())
} else if secs >= MINUTE {
format!("{:.0} minutes", (secs / MINUTE).round())
} else {
format!("{:.0} seconds", secs.round())
}
}
fn main() { fn main() {
env_logger::init(); env_logger::init();
let args = Args::parse(); let args = Args::parse();
@ -98,9 +77,7 @@ fn main() {
let pb = ProgressBar::new(n_steps.try_into().unwrap()); let pb = ProgressBar::new(n_steps.try_into().unwrap());
pb.set_style( pb.set_style(
ProgressStyle::with_template("[{elapsed_precise}] {bar:40.cyan/blue} {percent_precise}% \n\ ProgressStyle::with_template("[{elapsed_precise}] {bar:40.cyan/blue} {percent_precise}% {eta} {msg}")
Time remaining: {eta} \n\
Current simulation speed: {msg}")
.unwrap() .unwrap()
.progress_chars("=>-"), .progress_chars("=>-"),
); );
@ -111,12 +88,5 @@ fn main() {
args.steps_per_save, args.steps_per_save,
args.output_file, args.output_file,
); );
let start = Instant::now(); sim.run(n_steps, Some(|| pb.inc(args.steps_per_save as u64)));
sim.run(n_steps, Some(|| {
let elapsed = start.elapsed().as_secs() as f64 + 1.0;
let speed = Duration::from_secs_f64(pb.position() as f64 * args.step_size / elapsed);
pb.set_message(format!("{} /sec", format_duration_single_unit(speed)));
pb.inc(args.steps_per_save as u64);
})
);
} }

View File

@ -1,15 +1,15 @@
use std::fs::OpenOptions; use std::fs::OpenOptions;
use std::io::BufWriter; use std::io::BufWriter;
use serde::Serialize; use serde::{Deserialize, Serialize};
use log::{debug, trace}; use log::{info, warn, error, debug, trace};
use glam::DVec3; use glam::DVec3;
use crate::config::Body; use crate::config::Body;
use crate::types::{norm_mass, norm_pos, norm_vel, norm_time, real_pos, real_vel, real_time, real_body}; use crate::types::{norm_mass, norm_pos, norm_vel, norm_time, real_pos, real_vel, real_time};
pub struct Simulation { pub struct Simulation {
pub bodies: Vec<Body>, bodies: Vec<Body>,
step_size: f64, step_size: f64,
steps_per_save: usize, steps_per_save: usize,
save_file: String save_file: String
@ -53,13 +53,12 @@ impl Simulation {
self.step_bodies(); self.step_bodies();
if i % self.steps_per_save == 0 { if i % self.steps_per_save == 0 {
//save the state //save the state
let real_bodies: Vec<Body> = self.bodies.iter().map(real_body).collect();
let snapshot = Snapshot { let snapshot = Snapshot {
time: real_time(i as f64*self.step_size), time: real_time(i as f64*self.step_size),
bodies: &real_bodies, bodies: &self.bodies,
}; };
bincode::serialize_into(&mut writer, &snapshot).expect("Couldn't write to trajectory. "); bincode::serialize_into(&mut writer, &snapshot);
//Do the progress bar //Do the progress bar
if let Some(f) = &mut on_step { if let Some(f) = &mut on_step {
f(); f();

View File

@ -91,15 +91,4 @@ pub fn norm_body(body: Body) -> Body {
velocity: norm_vel(body.velocity), velocity: norm_vel(body.velocity),
acceleration: DVec3::ZERO, acceleration: DVec3::ZERO,
} }
}
#[inline]
pub fn real_body(body: &Body) -> Body {
Body {
name: body.name.clone(),
mass: real_mass(body.mass),
position: real_pos(body.position),
velocity: real_vel(body.velocity),
acceleration: DVec3::ZERO,
}
} }

Binary file not shown.

Binary file not shown.