use std::cell::RefCell;
use crate::math::array::Array;
use crate::methods::finitedifferences::StepCondition;
use crate::types::Time;
#[derive(Debug)]
pub struct FdmSnapshotCondition {
t: Time,
values: RefCell<Array>,
}
impl FdmSnapshotCondition {
pub fn new(t: Time) -> Self {
Self {
t,
values: RefCell::new(Array::new()),
}
}
pub fn time(&self) -> Time {
self.t
}
pub fn values(&self) -> Array {
self.values.borrow().clone()
}
}
impl StepCondition for FdmSnapshotCondition {
fn apply_to(&self, a: &mut Array, t: Time) {
if t == self.t {
*self.values.borrow_mut() = a.clone();
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn values_are_empty_before_the_capture_fires() {
let condition = FdmSnapshotCondition::new(1.5);
assert!(condition.values().is_empty());
assert_eq!(condition.time(), 1.5);
}
#[test]
fn a_time_other_than_the_capture_time_records_nothing() {
let condition = FdmSnapshotCondition::new(1.5);
let mut values = Array::from([1.0, -2.5, 4.0]);
let before = values.clone();
condition.apply_to(&mut values, 1.25);
assert_eq!(values, before);
assert!(condition.values().is_empty());
}
#[test]
fn the_capture_time_records_the_whole_grid_unchanged() {
let condition = FdmSnapshotCondition::new(1.5);
let mut values = Array::from([1.0, -2.5, 4.0]);
let before = values.clone();
condition.apply_to(&mut values, 1.5);
assert_eq!(values, before);
assert_eq!(condition.values(), before);
}
#[test]
fn a_later_time_does_not_overwrite_the_capture() {
let condition = FdmSnapshotCondition::new(1.5);
let mut captured = Array::from([1.0, -2.5, 4.0]);
let mut other = Array::from([9.0, 9.0, 9.0]);
condition.apply_to(&mut captured, 1.5);
condition.apply_to(&mut other, 2.0);
assert_eq!(condition.values(), Array::from([1.0, -2.5, 4.0]));
}
}