use tpt_math_linalg::tpt_math_linalg_dense::{DMatrix, DVector};
use tpt_sci_grid::{Boundary, UniformGrid1D, laplacian_1d};
use crate::SimError;
use crate::submodel::SubModel;
pub struct DiffusionSubModel {
id: String,
laplacian: DMatrix,
coeff: f64,
state: Vec<f64>,
input: Vec<f64>,
time: f64,
max_step: f64,
}
impl DiffusionSubModel {
pub fn new(
id: &str,
grid: UniformGrid1D,
coeff: f64,
bc: Boundary,
u0: Vec<f64>,
) -> Result<Self, SimError> {
if u0.len() != grid.n() {
return Err(SimError::Advance(
id.to_string(),
format!(
"initial field length {} != grid size {}",
u0.len(),
grid.n()
),
));
}
let stability = grid.dx() * grid.dx() / (2.0 * coeff.max(f64::MIN_POSITIVE));
Ok(Self {
id: id.to_string(),
laplacian: laplacian_1d(&grid, bc),
coeff,
state: u0.clone(),
input: vec![0.0; u0.len()],
time: 0.0,
max_step: 0.9 * stability,
})
}
pub fn coeff(&self) -> f64 {
self.coeff
}
pub fn set_max_step(&mut self, max_step: f64) {
self.max_step = max_step;
}
}
impl SubModel for DiffusionSubModel {
fn id(&self) -> &str {
&self.id
}
fn time(&self) -> f64 {
self.time
}
fn max_step(&self) -> f64 {
self.max_step
}
fn advance(&mut self, dt: f64) -> Result<(), SimError> {
let u = DVector::from_vec(self.state.clone());
let lap = self.laplacian.clone() * u;
let mut next = self.state.clone();
for i in 0..next.len() {
next[i] += dt * (self.coeff * lap[i] + self.input[i]);
}
self.state = next;
self.time += dt;
Ok(())
}
fn state(&self) -> &[f64] {
&self.state
}
fn input_mut(&mut self) -> Option<&mut [f64]> {
Some(&mut self.input)
}
fn restore_state(&mut self, state: &[f64], time: f64) -> Result<(), SimError> {
if state.len() != self.state.len() {
return Err(SimError::Advance(
self.id.clone(),
format!(
"checkpoint state length {} != model state length {}",
state.len(),
self.state.len()
),
));
}
self.state = state.to_vec();
self.time = time;
Ok(())
}
}