From 5d59a51bbe5ccc84d5aee10597b90f774292d429 Mon Sep 17 00:00:00 2001 From: Magnus Ulimoen Date: Fri, 12 Feb 2021 18:28:30 +0100 Subject: [PATCH] Add option to output information in JSON --- multigrid/src/main.rs | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/multigrid/src/main.rs b/multigrid/src/main.rs index cff42d9..7d8931c 100644 --- a/multigrid/src/main.rs +++ b/multigrid/src/main.rs @@ -191,6 +191,19 @@ struct Options { /// Print error at the end of the run #[structopt(long)] error: bool, + /// Output information regarding time elapsed and error + /// in json format + #[structopt(long)] + output_json: bool, +} + +#[derive(Default, serde::Serialize)] +struct OutputInformation { + filename: std::path::PathBuf, + #[serde(skip_serializing_if = "Option::is_none")] + time_elapsed: Option, + #[serde(skip_serializing_if = "Option::is_none")] + error: Option, } fn main() { @@ -273,9 +286,14 @@ fn main() { } progressbar.finish_and_clear(); + let mut outinfo = OutputInformation { + filename: opt.output, + ..Default::default() + }; + if let Some(timer) = timer { let duration = timer.elapsed(); - println!("Time elapsed: {} seconds", duration.as_secs_f64()); + outinfo.time_elapsed = Some(duration); } output.add_timestep(ntime, &sys.fnow); @@ -287,7 +305,18 @@ fn main() { fvort.vortex(grid.x(), grid.y(), time, &vortexparams); e += fmod.h2_err(&fvort, &**op); } - println!("Total error: {:e}", e); + outinfo.error = Some(e); + } + + if opt.output_json { + println!("{}", json5::to_string(&outinfo).unwrap()); + } else { + if let Some(duration) = outinfo.time_elapsed { + println!("Time elapsed: {} seconds", duration.as_secs_f64()); + } + if let Some(error) = outinfo.error { + println!("Total error: {:e}", error); + } } }