diff options
author | Dennis Kobert <dennis@kobert.dev> | 2019-12-12 21:25:04 +0100 |
---|---|---|
committer | Dennis Kobert <dennis@kobert.dev> | 2019-12-12 21:25:04 +0100 |
commit | 5fa0d5d889dff17a8e011971d80de7661ae130eb (patch) | |
tree | fc6b27022e9d57a38391e0705d3e49c41742f1c6 /src/plot | |
parent | aa98e4dba7d771bd1e9519442f830542cff2860c (diff) |
Add rust code
Diffstat (limited to 'src/plot')
-rw-r--r-- | src/plot/gnuplot.rs | 72 | ||||
-rw-r--r-- | src/plot/mod.rs | 9 |
2 files changed, 81 insertions, 0 deletions
diff --git a/src/plot/gnuplot.rs b/src/plot/gnuplot.rs new file mode 100644 index 0000000..b0d63d8 --- /dev/null +++ b/src/plot/gnuplot.rs @@ -0,0 +1,72 @@ +use std::io; +use std::io::Write; +use std::path::PathBuf; +use std::process; +use std::process::{Command, Stdio}; + +pub struct Gnuplot; + +impl Gnuplot { + fn exec_plot( + data: Vec<[f64; 6]>, + chunk_size: u32, + start: usize, + end: usize, + script: &str, + ) -> Result<process::Child, io::Error> { + let mut path = PathBuf::from("gnuplot"); + path.push(script); + let mut file = std::fs::File::create("data").expect("could not create data file"); + let process = Command::new("gnuplot") + .args(&[ + "-e", + format!("states={}; start={}; end={}", chunk_size, start, end).as_ref(), + "-c", + path.to_str().unwrap_or(script), + ]) + .stdin(process::Stdio::piped()) + .stdout(Stdio::piped()) + .spawn() + .expect("Failed to spawn child process"); + + //let stdin = process.stdin.as_mut().expect("Failed to open stdin"); + for line in data { + let line = format!( + "{}; {}; {}; {}; {}; {}\n", + line[0], line[1], line[2], line[3], line[4], line[5] + ); + file.write_all(line.as_bytes()) + .expect("Failed to write to stdin"); + } + Ok(process) + } +} + +impl super::Graph for Gnuplot { + fn plot3d(data: Vec<[f64; 6]>, chunk_size: u32) { + let iterations = data.len() / chunk_size as usize; + match Gnuplot::exec_plot(data, chunk_size, 1, iterations, "anim3d.plt") { + Ok(mut process) => println!("opening gnuplot {:?}", process.wait()), + Err(err) => println!("failed to start gnuplot: {}", err), + } + } + fn animate3d(data: Vec<[f64; 6]>, chunk_size: u32, start: usize, end: usize) { + match Gnuplot::exec_plot(data, chunk_size, start, end, "anim3d.plt") { + Ok(mut process) => println!("opening gnuplot {:?}", process.wait()), + Err(err) => println!("failed to start gnuplot: {}", err), + } + } + fn plot2d(data: Vec<[f64; 6]>, chunk_size: u32) { + let iterations = data.len() / chunk_size as usize; + match Gnuplot::exec_plot(data, chunk_size, 1, iterations, "anim2d.plt") { + Ok(mut process) => println!("opening gnuplot {:?}", process.wait()), + Err(err) => println!("failed to start gnuplot: {}", err), + } + } + fn animate2d(data: Vec<[f64; 6]>, chunk_size: u32, start: usize, end: usize) { + match Gnuplot::exec_plot(data, chunk_size, start, end, "anim2d.plt") { + Ok(mut process) => println!("opening gnuplot {:?}", process.wait()), + Err(err) => println!("failed to start gnuplot: {}", err), + } + } +} diff --git a/src/plot/mod.rs b/src/plot/mod.rs new file mode 100644 index 0000000..8773020 --- /dev/null +++ b/src/plot/mod.rs @@ -0,0 +1,9 @@ +pub mod gnuplot; +pub use gnuplot::Gnuplot; + +pub trait Graph { + fn plot3d(data: Vec<[f64; 6]>, chunk_size: u32); + fn animate3d(data: Vec<[f64; 6]>, chunk_size: u32, start: usize, end: usize); + fn plot2d(data: Vec<[f64; 6]>, chunk_size: u32); + fn animate2d(data: Vec<[f64; 6]>, chunk_size: u32, start: usize, end: usize); +} |