working with nice progress bar and trajectory files
This commit is contained in:
parent
b1b2e51507
commit
6cb8d8e450
38
src/main.rs
38
src/main.rs
@ -3,7 +3,7 @@ use std::error::Error;
|
||||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
use std::path::Path;
|
||||
use std::time::Duration;
|
||||
use std::time::{Duration,Instant};
|
||||
|
||||
// External crates
|
||||
use clap::Parser;
|
||||
@ -18,7 +18,7 @@ mod simulator;
|
||||
|
||||
// Specific uses from modules
|
||||
use crate::simulator::Simulation;
|
||||
use crate::types::norm_time;
|
||||
use crate::types::{norm_time, real_body};
|
||||
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
@ -60,6 +60,27 @@ fn read_config<P: AsRef<Path>>(path: P) -> Result<config::ConfigFile, Box<dyn Er
|
||||
}
|
||||
//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() {
|
||||
env_logger::init();
|
||||
let args = Args::parse();
|
||||
@ -77,7 +98,9 @@ fn main() {
|
||||
|
||||
let pb = ProgressBar::new(n_steps.try_into().unwrap());
|
||||
pb.set_style(
|
||||
ProgressStyle::with_template("[{elapsed_precise}] {bar:40.cyan/blue} {percent_precise}% {eta} {msg}")
|
||||
ProgressStyle::with_template("[{elapsed_precise}] {bar:40.cyan/blue} {percent_precise}% \n\
|
||||
Time remaining: {eta} \n\
|
||||
Current simulation speed: {msg}")
|
||||
.unwrap()
|
||||
.progress_chars("=>-"),
|
||||
);
|
||||
@ -88,5 +111,12 @@ fn main() {
|
||||
args.steps_per_save,
|
||||
args.output_file,
|
||||
);
|
||||
sim.run(n_steps, Some(|| pb.inc(args.steps_per_save as u64)));
|
||||
let start = Instant::now();
|
||||
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);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
@ -6,10 +6,10 @@ use log::{debug, trace};
|
||||
use glam::DVec3;
|
||||
|
||||
use crate::config::Body;
|
||||
use crate::types::{norm_mass, norm_pos, norm_vel, norm_time, real_pos, real_vel, real_time};
|
||||
use crate::types::{norm_mass, norm_pos, norm_vel, norm_time, real_pos, real_vel, real_time, real_body};
|
||||
|
||||
pub struct Simulation {
|
||||
bodies: Vec<Body>,
|
||||
pub bodies: Vec<Body>,
|
||||
step_size: f64,
|
||||
steps_per_save: usize,
|
||||
save_file: String
|
||||
@ -53,9 +53,10 @@ impl Simulation {
|
||||
self.step_bodies();
|
||||
if i % self.steps_per_save == 0 {
|
||||
//save the state
|
||||
let real_bodies: Vec<Body> = self.bodies.iter().map(real_body).collect();
|
||||
let snapshot = Snapshot {
|
||||
time: real_time(i as f64*self.step_size),
|
||||
bodies: &self.bodies,
|
||||
bodies: &real_bodies,
|
||||
};
|
||||
|
||||
bincode::serialize_into(&mut writer, &snapshot).expect("Couldn't write to trajectory. ");
|
||||
|
11
src/types.rs
11
src/types.rs
@ -91,4 +91,15 @@ pub fn norm_body(body: Body) -> Body {
|
||||
velocity: norm_vel(body.velocity),
|
||||
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,
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user