Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ Cargo.lock
output
input
simulation.log
std*.log
64 changes: 47 additions & 17 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -57,6 +57,18 @@ struct PlainRecord {
data: R64,
}


pub trait SaveFile {
fn write(&self, config: &Config) -> Result<()>;
}

//impl<T: FileType> SaveFile for T {
// fn write<T>() -> Result<()> {

// }
//}
//

/// Simply prints the Wafer banner with current commit info and thread count.
pub fn print_banner(sha: &str) {
println!(" {}", Blue.paint("___"));
Expand All @@ -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<R64>, 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),
}
}
}

Expand Down Expand Up @@ -140,6 +153,23 @@ pub fn potential_sub(config: &Config) -> Result<()> {
Ok(())
}

//trait WriteArray {
//fn write(array: &ArrayView3<R64>, filename: &str, err_kind: ErrorKind) -> Result<()>;
//}

//impl WriteArray for config::FileType {
//fn write(array: &ArrayView3<R64>, 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
Expand Down
22 changes: 13 additions & 9 deletions src/potential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<R64>;

#[derive(Debug)]
/// Holds the potential arrays for the current simulation.
pub struct Potentials {
/// The potential.
pub v: Array3<R64>,
pub v: Potential,
/// Ancillary array `a`.
pub a: Array3<R64>,
pub a: Potential,
/// Ancillary array `b`.
pub b: Array3<R64>,
pub b: Potential,
/// Potsub value.
pub pot_sub: (Option<Array3<R64>>, Option<R64>),
pub pot_sub: (Option<Potential>, Option<R64>),
}

#[derive(Debug, Deserialize, Serialize)]
Expand All @@ -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<Array3<R64>> {
pub fn generate(config: &Config) -> Result<Potential> {
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];
Expand Down Expand Up @@ -76,7 +79,7 @@ pub fn load_arrays(config: &Config, log: &Logger) -> Result<Potentials> {
let mut minima = r64(MAX);
let bb = config.central_difference.bb();
let num = &config.grid.size;
let v: Array3<R64> = 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");
Expand Down Expand Up @@ -162,8 +165,9 @@ pub fn load_arrays(config: &Config, log: &Logger) -> Result<Potentials> {

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) {
Expand Down
6 changes: 3 additions & 3 deletions wafer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down