From d23e2f0e4980003010d1ff2acaa889057d291bac Mon Sep 17 00:00:00 2001 From: Tim DuBois Date: Mon, 23 Jul 2018 22:34:03 +0200 Subject: [PATCH] Refactor to involve more traits --- .gitignore | 1 + src/output.rs | 64 +++++++++++++++++++++++++++++++++++------------- src/potential.rs | 22 ++++++++++------- wafer.yaml | 6 ++--- 4 files changed, 64 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index fd6e458..3ad027d 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ Cargo.lock output input simulation.log +std*.log diff --git a/src/output.rs b/src/output.rs index 35cb44c..1971fe4 100644 --- a/src/output.rs +++ b/src/output.rs @@ -16,10 +16,10 @@ use std::io::prelude::*; use term_size; use yansi::Color::Blue; -use config::{Config, FileType, Index3}; +use config::{self, Config, FileType, Index3}; use errors::*; use grid; -use potential::{self, PotentialSubSingle}; +use potential::{self, Potential, PotentialSubSingle}; lazy_static! { /// Date & time at which the simulation was started. Used as a unique identifier for @@ -57,6 +57,18 @@ struct PlainRecord { data: R64, } + +pub trait SaveFile { + fn write(&self, config: &Config) -> Result<()>; +} + +//impl SaveFile for T { +// fn write() -> Result<()> { + +// } +//} +// + /// Simply prints the Wafer banner with current commit info and thread count. pub fn print_banner(sha: &str) { println!(" {}", Blue.paint("___")); @@ -79,21 +91,22 @@ pub fn print_banner(sha: &str) { /// Handles the saving of potential data to disk. /// /// # Arguments -/// * `v` - The potential to output. -/// * `project` - The project name (for directory to save to). -/// * `file_type` - What type of file format to use in the output. -pub fn potential(v: &ArrayView3, project: &str, file_type: &FileType) -> Result<()> { - let filename = format!( - "{}/potential{}", - get_project_dir(project), - file_type.extentsion() - ); - match *file_type { - FileType::Messagepack => write_mpk(v, &filename, ErrorKind::SavePotential), - FileType::Csv => write_csv(v, &filename), - FileType::Json => write_json(v, &filename, ErrorKind::SavePotential), - FileType::Yaml => write_yaml(v, &filename, ErrorKind::SavePotential), - FileType::Ron => write_ron(v, &filename, ErrorKind::SavePotential), +/// * `config` - The configuration struct. +impl SaveFile for Potential { + fn write(&self, config: &Config) -> Result<()> { + let work = grid::get_work_area(&self, config.central_difference.ext()); + let filename = format!( + "{}/potential{}", + get_project_dir(&config.project_name), + config.output.file_type.extentsion() + ); + match config.output.file_type { + FileType::Messagepack => write_mpk(&work, &filename, ErrorKind::SavePotential), + FileType::Csv => write_csv(&work, &filename), + FileType::Json => write_json(&work, &filename, ErrorKind::SavePotential), + FileType::Yaml => write_yaml(&work, &filename, ErrorKind::SavePotential), + FileType::Ron => write_ron(&work, &filename, ErrorKind::SavePotential), + } } } @@ -140,6 +153,23 @@ pub fn potential_sub(config: &Config) -> Result<()> { Ok(()) } +//trait WriteArray { + //fn write(array: &ArrayView3, filename: &str, err_kind: ErrorKind) -> Result<()>; +//} + +//impl WriteArray for config::FileType { + //fn write(array: &ArrayView3, filename: &str, err_kind: ErrorKind) -> Result<()> { + //let mut output = Vec::new(); + //array + //.serialize(&mut rmps::Serializer::new(&mut output)) + //.chain_err(|| ErrorKind::Serialize)?; + //let mut buffer = + //File::create(filename).chain_err(|| ErrorKind::CreateFile(filename.to_string()))?; + //buffer.write_all(&output).chain_err(|| err_kind)?; + //Ok(()) + //} +//} + /// Outputs an array to disk in a plain, csv format /// /// # Arguments diff --git a/src/potential.rs b/src/potential.rs index 3e68800..9087856 100644 --- a/src/potential.rs +++ b/src/potential.rs @@ -7,21 +7,24 @@ use std::f64::MAX; use config::{Config, Grid, Index3, PotentialType}; use errors::*; -use grid; use input; use output; +use output::SaveFile; + + +pub type Potential = Array3; #[derive(Debug)] /// Holds the potential arrays for the current simulation. pub struct Potentials { /// The potential. - pub v: Array3, + pub v: Potential, /// Ancillary array `a`. - pub a: Array3, + pub a: Potential, /// Ancillary array `b`. - pub b: Array3, + pub b: Potential, /// Potsub value. - pub pot_sub: (Option>, Option), + pub pot_sub: (Option, Option), } #[derive(Debug, Deserialize, Serialize)] @@ -43,7 +46,7 @@ pub struct PotentialSubSingle { /// /// A 3D array of potential values of the requested size. /// Or an error if called on the wrong potential type. -pub fn generate(config: &Config) -> Result> { +pub fn generate(config: &Config) -> Result { let num = &config.grid.size; let bb = config.central_difference.bb(); let init_size: [usize; 3] = [num.x + bb, num.y + bb, num.z + bb]; @@ -76,7 +79,7 @@ pub fn load_arrays(config: &Config, log: &Logger) -> Result { let mut minima = r64(MAX); let bb = config.central_difference.bb(); let num = &config.grid.size; - let v: Array3 = match config.potential { + let v: Potential = match config.potential { PotentialType::FromFile => { let init_size: [usize; 3] = [num.x + bb, num.y + bb, num.z + bb]; info!(log, "Loading potential from file"); @@ -162,8 +165,9 @@ pub fn load_arrays(config: &Config, log: &Logger) -> Result { if config.output.save_potential { info!(log, "Saving potential to disk"); - let work = grid::get_work_area(&v, config.central_difference.ext()); - if let Err(err) = output::potential(&work, &config.project_name, &config.output.file_type) { +// let work = grid::get_work_area(&v, config.central_difference.ext()); +// if let Err(err) = output::potential(&work, &config.project_name, &config.output.file_type) { + if let Err(err) = v.write(&config) { warn!(log, "Could not write potential to disk: {}", err); } if let Err(err) = output::potential_sub(&config) { diff --git a/wafer.yaml b/wafer.yaml index f25a98b..afa30c2 100644 --- a/wafer.yaml +++ b/wafer.yaml @@ -15,9 +15,9 @@ project_name: develop # Number of spatial grid points, step sizes in space and time. grid: size: # Number of grid points in Cartesian coordinates. - x: 50 - y: 50 - z: 50 + x: 10 + y: 10 + z: 10 dn: 0.01 # Spatial step dt: 3e-5 # Temporal step. For stability this must be < dn*dn/3.