From f8b073f1201c53c4625bbae758f74677f40ceb27 Mon Sep 17 00:00:00 2001 From: Daniel Bauer Date: Sun, 14 Oct 2018 11:49:39 +0200 Subject: [PATCH] buffer state dumps and write results --- src/io.rs | 9 +++++---- src/lib.rs | 15 +++++++++------ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/io.rs b/src/io.rs index a6b89f0..d1a172f 100644 --- a/src/io.rs +++ b/src/io.rs @@ -3,7 +3,7 @@ use super::histogram::Histogram; use super::Config; use std::fs::File; use std::io::prelude::*; -use std::io::BufReader; +use std::io::{BufReader,BufWriter}; use k_B; use std::process; use std::option::Option; @@ -176,13 +176,14 @@ fn read_window_file(window_file: &str, cfg: &Config) -> Option { // TODO multidimensional output pub fn write_results(out_file: &str, ds: &Dataset, free: &Vec, prob: &Vec) -> Result<(), Box> { - let mut output = File::create(out_file)?; - writeln!(output, "#{}\t{}\t{}", "x", "Free Energy", "Probability"); // TODO better format (coord1, coord2..) + let output = File::create(out_file)?; + let mut buf = BufWriter::new(output); + writeln!(buf, "#{}\t{}\t{}", "x", "Free Energy", "Probability"); // TODO better format (coord1, coord2..) for bin in 0..free.len() { let coords = ds.get_coords_for_bin(bin); let coords_str: String = coords.iter().map(|c| {format!("{:8.6}", c)}) .collect::>().join("\t"); - writeln!(output, "{}\t{:8.6}\t{:8.6}", coords_str, free[bin], prob[bin])?; + writeln!(buf, "{}\t{:8.6}\t{:8.6}", coords_str, free[bin], prob[bin])?; } Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index ec418e7..a2c37db 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,7 @@ use std::result::Result; use histogram::{Dataset,Histogram}; use std::f64; use std::fmt; +use std::io::prelude::*; #[allow(non_upper_case_globals)] static k_B: f64 = 0.0083144621; // kJ/mol*K @@ -237,16 +238,18 @@ pub fn run(cfg: &Config) -> Result<(), Box>{ } fn dump_state(ds: &Dataset, F: &Vec, F_prev: &Vec, P: &Vec, A: &Vec) { - println!("# PMF"); - println!("#x\t\tFree Energy\t\tP(x)"); + let out = std::io::stdout(); + let mut lock = out.lock(); + writeln!(lock, "# PMF"); + writeln!(lock, "#x\t\tFree Energy\t\tP(x)"); for bin in 0..ds.num_bins { let x = ds.get_coords_for_bin(bin)[0]; // TODO - println!("{:9.5}\t{:9.5}\t{:9.5}", x, A[bin], P[bin]); + writeln!(lock, "{:9.5}\t{:9.5}\t{:9.5}", x, A[bin], P[bin]); } - println!("# Bias offsets"); - println!("#Window\t\tF\t\tdF"); + writeln!(lock, "# Bias offsets"); + writeln!(lock, "#Window\t\tF\t\tdF"); for window in 0..ds.num_windows { - println!("{}\t{:9.5}\t{:8.8}", window, F[window], (F[window]-F_prev[window]).abs()); + writeln!(lock, "{}\t{:9.5}\t{:8.8}", window, F[window], (F[window]-F_prev[window]).abs()); } }