pub const WGSL_RK45_F64: &str = include_str!("../shaders/math/rk45_f64.wgsl");
use crate::error::{BarracudaError, Result};
#[derive(Debug, Clone)]
pub struct Rk45Config {
pub rtol: f64,
pub atol: f64,
pub h_init: f64,
pub h_min: f64,
pub h_max: f64,
pub max_steps: usize,
pub safety: f64,
}
impl Default for Rk45Config {
fn default() -> Self {
Self {
rtol: 1e-6,
atol: 1e-9,
h_init: 0.01,
h_min: 1e-12,
h_max: 1.0,
max_steps: 100_000,
safety: 0.9,
}
}
}
impl Rk45Config {
#[must_use]
pub fn new(rtol: f64, atol: f64) -> Self {
Self {
rtol,
atol,
..Default::default()
}
}
#[must_use]
pub fn with_h_init(mut self, h: f64) -> Self {
self.h_init = h;
self
}
#[must_use]
pub fn with_step_bounds(mut self, h_min: f64, h_max: f64) -> Self {
self.h_min = h_min;
self.h_max = h_max;
self
}
#[must_use]
pub fn with_max_steps(mut self, max_steps: usize) -> Self {
self.max_steps = max_steps;
self
}
#[must_use]
pub fn with_safety(mut self, safety: f64) -> Self {
self.safety = safety;
self
}
}
#[derive(Debug)]
pub struct Rk45Result {
pub t_final: f64,
pub y_final: Vec<f64>,
pub t_history: Vec<f64>,
pub y_history: Vec<Vec<f64>>,
pub n_steps: usize,
pub n_rejected: usize,
pub h_final: f64,
}
impl Rk45Result {
#[must_use]
pub fn n_vars(&self) -> usize {
self.y_final.len()
}
#[must_use]
pub fn variable_trajectory(&self, var_idx: usize) -> Vec<f64> {
self.y_history
.iter()
.filter_map(|step| step.get(var_idx).copied())
.collect()
}
}
pub fn rk45_solve<F>(
f: &F,
t_start: f64,
t_end: f64,
y0: &[f64],
config: &Rk45Config,
) -> Result<Rk45Result>
where
F: Fn(f64, &[f64]) -> Vec<f64>,
{
if t_end <= t_start {
return Err(BarracudaError::InvalidInput {
message: "t_end must be > t_start".to_string(),
});
}
if y0.is_empty() {
return Err(BarracudaError::InvalidInput {
message: "y0 must not be empty".to_string(),
});
}
let n = y0.len();
let mut t = t_start;
let mut y = y0.to_vec();
let mut h = config.h_init.min(t_end - t_start);
let mut t_history = vec![t];
let mut y_history = vec![y.clone()];
let mut n_steps = 0;
let mut n_rejected = 0;
const A21: f64 = 1.0 / 5.0;
const A31: f64 = 3.0 / 40.0;
const A32: f64 = 9.0 / 40.0;
const A41: f64 = 44.0 / 45.0;
const A42: f64 = -56.0 / 15.0;
const A43: f64 = 32.0 / 9.0;
const A51: f64 = 19372.0 / 6561.0;
const A52: f64 = -25360.0 / 2187.0;
const A53: f64 = 64448.0 / 6561.0;
const A54: f64 = -212.0 / 729.0;
const A61: f64 = 9017.0 / 3168.0;
const A62: f64 = -355.0 / 33.0;
const A63: f64 = 46732.0 / 5247.0;
const A64: f64 = 49.0 / 176.0;
const A65: f64 = -5103.0 / 18656.0;
const A71: f64 = 35.0 / 384.0;
const A73: f64 = 500.0 / 1113.0;
const A74: f64 = 125.0 / 192.0;
const A75: f64 = -2187.0 / 6784.0;
const A76: f64 = 11.0 / 84.0;
const E1: f64 = 71.0 / 57_600.0;
const E3: f64 = -71.0 / 16_695.0;
const E4: f64 = 71.0 / 1920.0;
const E5: f64 = -17_253.0 / 339_200.0;
const E6: f64 = 22.0 / 525.0;
const E7: f64 = -1.0 / 40.0;
while t < t_end && n_steps < config.max_steps {
if t + h > t_end {
h = t_end - t;
}
let k1 = f(t, &y);
let y2: Vec<f64> = y
.iter()
.zip(k1.iter())
.map(|(yi, k1i)| (h * A21).mul_add(*k1i, *yi))
.collect();
let k2 = f(t + h / 5.0, &y2);
let y3: Vec<f64> = y
.iter()
.zip(k1.iter())
.zip(k2.iter())
.map(|((yi, k1i), k2i)| h.mul_add(A31.mul_add(*k1i, A32 * *k2i), *yi))
.collect();
let k3 = f(t + 3.0 * h / 10.0, &y3);
let y4: Vec<f64> = (0..n)
.map(|i| h.mul_add(A43.mul_add(k3[i], A41.mul_add(k1[i], A42 * k2[i])), y[i]))
.collect();
let k4 = f(t + 4.0 * h / 5.0, &y4);
let y5: Vec<f64> = (0..n)
.map(|i| {
h.mul_add(
A53.mul_add(k3[i], A51.mul_add(k1[i], A52.mul_add(k2[i], A54 * k4[i]))),
y[i],
)
})
.collect();
let k5 = f(t + 8.0 * h / 9.0, &y5);
let y6: Vec<f64> = (0..n)
.map(|i| {
h.mul_add(
A63.mul_add(
k3[i],
A61.mul_add(k1[i], A62.mul_add(k2[i], A64.mul_add(k4[i], A65 * k5[i]))),
),
y[i],
)
})
.collect();
let k6 = f(t + h, &y6);
let y_new: Vec<f64> = (0..n)
.map(|i| {
h.mul_add(
A74.mul_add(
k4[i],
A71.mul_add(k1[i], A73.mul_add(k3[i], A75.mul_add(k5[i], A76 * k6[i]))),
),
y[i],
)
})
.collect();
let k7 = f(t + h, &y_new);
let error: Vec<f64> = (0..n)
.map(|i| {
h.mul_add(
E4.mul_add(
k4[i],
E1.mul_add(
k1[i],
E3.mul_add(k3[i], E5.mul_add(k5[i], E6.mul_add(k6[i], E7 * k7[i]))),
),
),
0.0,
)
})
.collect();
let err_norm = error
.iter()
.zip(y_new.iter())
.map(|(e, yi)| {
let scale = config.rtol.mul_add(yi.abs(), config.atol);
(e / scale).powi(2)
})
.sum::<f64>()
.sqrt()
/ (n as f64).sqrt();
if err_norm <= 1.0 {
t += h;
y = y_new;
t_history.push(t);
y_history.push(y.clone());
n_steps += 1;
if err_norm > 0.0 {
let h_new = h * config.safety * (1.0 / err_norm).powf(0.2);
h = h_new.clamp(config.h_min, config.h_max);
} else {
h = config.h_max;
}
} else {
let h_new = h * config.safety * (1.0 / err_norm).powf(0.25);
h = h_new.max(config.h_min);
n_rejected += 1;
if h <= config.h_min {
return Err(BarracudaError::Numerical {
message: format!("Step size {} below minimum {} at t={}", h, config.h_min, t),
});
}
}
}
if n_steps >= config.max_steps {
return Err(BarracudaError::Numerical {
message: format!("Max steps {} exceeded", config.max_steps),
});
}
Ok(Rk45Result {
t_final: t,
y_final: y,
t_history,
y_history,
n_steps,
n_rejected,
h_final: h,
})
}
pub fn rk45_at<F>(
f: &F,
t_start: f64,
t_end: f64,
y0: &[f64],
config: &Rk45Config,
) -> Result<Vec<f64>>
where
F: Fn(f64, &[f64]) -> Vec<f64>,
{
let result = rk45_solve(f, t_start, t_end, y0, config)?;
Ok(result.y_final)
}
#[cfg(test)]
#[path = "rk45_tests.rs"]
mod tests;