add progressbar

This commit is contained in:
Magnus Ulimoen 2020-04-02 22:32:07 +02:00
parent d80a9bfa6d
commit 18ebbfde75
3 changed files with 21 additions and 4 deletions

View File

@ -20,6 +20,7 @@ f32 = []
[dev-dependencies] [dev-dependencies]
criterion = "0.3.0" criterion = "0.3.0"
structopt = "0.3.12" structopt = "0.3.12"
indicatif = "0.14.0"
[[bench]] [[bench]]
name = "maxwell" name = "maxwell"

View File

@ -158,6 +158,8 @@ impl<T: operators::UpwindOperator> System<T> {
#[derive(Debug, StructOpt)] #[derive(Debug, StructOpt)]
struct Options { struct Options {
json: std::path::PathBuf, json: std::path::PathBuf,
#[structopt(long, help = "Disable the progressbar")]
no_progressbar: bool,
} }
fn main() { fn main() {
@ -189,6 +191,7 @@ fn main() {
for grid in jgrids { for grid in jgrids {
grids.push(grid::Grid::new(grid.x, grid.y).unwrap()); grids.push(grid::Grid::new(grid.x, grid.y).unwrap());
} }
let integration_time: f64 = json["integration_time"].as_number().unwrap().into();
let mut sys = System::new(grids, bt); let mut sys = System::new(grids, bt);
sys.vortex( sys.vortex(
@ -207,11 +210,24 @@ fn main() {
let max_ny = sys.grids.iter().map(|g| g.ny()).max().unwrap(); let max_ny = sys.grids.iter().map(|g| g.ny()).max().unwrap();
std::cmp::max(max_nx, max_ny) std::cmp::max(max_nx, max_ny)
}; };
let t: f64 = json["integration_time"].as_number().unwrap().into();
let dt = 0.2 / (max_n as Float); let dt = 0.2 / (max_n as Float);
for _ in 0..(t / dt) as _ {
let ntime = (integration_time / dt).round() as usize;
let bar = if opt.no_progressbar {
indicatif::ProgressBar::hidden()
} else {
let bar = indicatif::ProgressBar::new(ntime as _);
bar.with_style(
indicatif::ProgressStyle::default_bar()
.template("{wide_bar:.cyan/blue} {pos}/{len} ({eta})"),
)
};
for _ in 0..ntime {
bar.inc(1);
sys.advance(dt); sys.advance(dt);
} }
bar.finish();
dump_to_file(&sys); dump_to_file(&sys);
} }

View File

@ -1,4 +1,5 @@
use crate::Float; use crate::Float;
use json::JsonValue;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct SimpleGrid { pub struct SimpleGrid {
@ -27,8 +28,7 @@ pub struct SimpleGrid {
/// Optional parameters: /// Optional parameters:
/// * name (for relating boundaries) /// * name (for relating boundaries)
/// * dir{e,w,n,s} (for boundary terms) /// * dir{e,w,n,s} (for boundary terms)
pub fn json_to_grids(json: json::JsonValue) -> Result<Vec<SimpleGrid>, String> { pub fn json_to_grids(json: JsonValue) -> Result<Vec<SimpleGrid>, String> {
use json::JsonValue;
fn json_to_grid(mut grid: JsonValue) -> Result<SimpleGrid, String> { fn json_to_grid(mut grid: JsonValue) -> Result<SimpleGrid, String> {
#[derive(Debug)] #[derive(Debug)]
enum ArrayForm { enum ArrayForm {