mirror of
https://github.com/dnlbauer/WHAM.git
synced 2026-09-11 06:35:30 +00:00
cache bias values in Histograms
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
use std::cell::RefCell;
|
||||||
|
|
||||||
// One histogram
|
// One histogram
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -51,12 +52,6 @@ pub struct Dataset {
|
|||||||
// width of a bin in unit of x
|
// width of a bin in unit of x
|
||||||
pub bin_width: f32,
|
pub bin_width: f32,
|
||||||
|
|
||||||
// locations of biases
|
|
||||||
pub bias_x0: Vec<f32>,
|
|
||||||
|
|
||||||
// force constants of biases
|
|
||||||
pub bias_fc: Vec<f32>,
|
|
||||||
|
|
||||||
// value of kT
|
// value of kT
|
||||||
pub kT: f32,
|
pub kT: f32,
|
||||||
|
|
||||||
@@ -65,13 +60,35 @@ pub struct Dataset {
|
|||||||
|
|
||||||
// flag for cyclic reaction coordinates
|
// flag for cyclic reaction coordinates
|
||||||
pub cyclic: bool,
|
pub cyclic: bool,
|
||||||
|
|
||||||
|
// locations of biases
|
||||||
|
bias_x0: Vec<f32>,
|
||||||
|
|
||||||
|
// force constants of biases
|
||||||
|
bias_fc: Vec<f32>,
|
||||||
|
|
||||||
|
// bias value cache
|
||||||
|
bias: RefCell<Vec<Option<f32>>>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Dataset {
|
impl Dataset {
|
||||||
|
|
||||||
pub fn new(num_bins: usize, bin_width: f32, hist_min: f32, hist_max: f32, bias_x0: Vec<f32>, bias_fc: Vec<f32>, kT: f32, histograms: Vec<Histogram>, cyclic: bool) -> Dataset {
|
pub fn new(num_bins: usize, bin_width: f32, hist_min: f32, hist_max: f32, bias_x0: Vec<f32>, bias_fc: Vec<f32>, kT: f32, histograms: Vec<Histogram>, cyclic: bool) -> Dataset {
|
||||||
let num_windows = histograms.len();
|
let num_windows = histograms.len();
|
||||||
Dataset{num_windows, num_bins, bin_width, hist_min, hist_max, bias_x0, bias_fc, kT, histograms, cyclic}
|
let bias: RefCell<Vec<Option<f32>>> = RefCell::new(vec![None; num_bins*num_windows]);
|
||||||
|
Dataset{
|
||||||
|
num_windows,
|
||||||
|
num_bins,
|
||||||
|
bin_width,
|
||||||
|
hist_min,
|
||||||
|
hist_max,
|
||||||
|
kT,
|
||||||
|
histograms,
|
||||||
|
cyclic,
|
||||||
|
bias_x0,
|
||||||
|
bias_fc,
|
||||||
|
bias,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -79,6 +96,11 @@ impl Dataset {
|
|||||||
// if cyclic is true, lowest and highest bins are assumed to be
|
// if cyclic is true, lowest and highest bins are assumed to be
|
||||||
// neighbors
|
// neighbors
|
||||||
pub fn calc_bias(&self, bin: usize, window: usize) -> f32 {
|
pub fn calc_bias(&self, bin: usize, window: usize) -> f32 {
|
||||||
|
let ndx = bin + (self.num_bins*window);
|
||||||
|
let mut cache = self.bias.borrow_mut();
|
||||||
|
match cache[ndx] {
|
||||||
|
Some(val) => val,
|
||||||
|
None => {
|
||||||
let x = self.get_x_for_bin(bin);
|
let x = self.get_x_for_bin(bin);
|
||||||
let mut dx = (x-self.bias_x0[window]).abs();
|
let mut dx = (x-self.bias_x0[window]).abs();
|
||||||
if self.cyclic {
|
if self.cyclic {
|
||||||
@@ -87,7 +109,11 @@ impl Dataset {
|
|||||||
dx -= hist_len;
|
dx -= hist_len;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
0.5*self.bias_fc[window]*dx*dx
|
let bias = 0.5*self.bias_fc[window]*dx*dx;
|
||||||
|
cache[ndx] = Some(bias);
|
||||||
|
bias
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// get center x value for a bin
|
// get center x value for a bin
|
||||||
|
|||||||
@@ -193,8 +193,9 @@ mod tests {
|
|||||||
assert_eq!(cfg.hist_max, ds.hist_max);
|
assert_eq!(cfg.hist_max, ds.hist_max);
|
||||||
let expected_bin_width = (cfg.hist_max - cfg.hist_min)/cfg.num_bins as f32;
|
let expected_bin_width = (cfg.hist_max - cfg.hist_min)/cfg.num_bins as f32;
|
||||||
assert_eq!(expected_bin_width, ds.bin_width);
|
assert_eq!(expected_bin_width, ds.bin_width);
|
||||||
assert_eq!(vec![0.0, 1.0], ds.bias_x0);
|
// bias fields are private
|
||||||
assert_eq!(vec![100.0, 200.0], ds.bias_fc);
|
// assert_eq!(vec![0.0, 1.0], ds.bias_x0);
|
||||||
|
// assert_eq!(vec![100.0, 200.0], ds.bias_fc);
|
||||||
assert_eq!(cfg.temperature * k_B, ds.kT);
|
assert_eq!(cfg.temperature * k_B, ds.kT);
|
||||||
assert_eq!(2, ds.histograms.len())
|
assert_eq!(2, ds.histograms.len())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user