use super::{rk4, DynamicalSystem};
pub struct Kuramoto {
state: Vec<f64>, omega: Vec<f64>, pub coupling: f64, pub n: usize,
speed: f64,
order_param: f64, }
impl Kuramoto {
pub fn new(n: usize, coupling: f64) -> Self {
let omega: Vec<f64> = (0..n)
.map(|i| {
let u = (i as f64 + 0.5) / n as f64;
let u_safe = u.clamp(1e-6, 1.0 - 1e-6);
1.0 + 0.5 * (std::f64::consts::PI * (u_safe - 0.5)).tan()
})
.collect();
let state: Vec<f64> = (0..n)
.map(|i| 2.0 * std::f64::consts::PI * i as f64 / n as f64)
.collect();
Self {
state,
omega,
coupling,
n,
speed: 0.0,
order_param: 0.0,
}
}
pub fn order_parameter(&self) -> f64 {
self.order_param
}
fn compute_deriv(state: &[f64], omega: &[f64], coupling: f64) -> Vec<f64> {
let n = state.len();
let k_over_n = coupling / n as f64;
(0..n)
.map(|i| {
let coupling_sum: f64 = state.iter().map(|&th_j| (th_j - state[i]).sin()).sum();
omega[i] + k_over_n * coupling_sum
})
.collect()
}
}
impl DynamicalSystem for Kuramoto {
fn state(&self) -> &[f64] {
&self.state
}
fn dimension(&self) -> usize {
self.n
}
fn name(&self) -> &str {
"Kuramoto"
}
fn speed(&self) -> f64 {
self.speed
}
fn deriv_at(&self, state: &[f64]) -> Vec<f64> {
Self::compute_deriv(state, &self.omega, self.coupling)
}
fn set_state(&mut self, s: &[f64]) {
let n = self.state.len().min(s.len());
for i in 0..n {
if s[i].is_finite() {
self.state[i] = s[i];
}
}
}
fn step(&mut self, dt: f64) {
let omega = self.omega.clone();
let coupling = self.coupling;
let prev = self.state.clone();
rk4(&mut self.state, dt, |s| {
Self::compute_deriv(s, &omega, coupling)
});
for th in &mut self.state {
*th = th.rem_euclid(std::f64::consts::TAU);
}
let (sin_sum, cos_sum): (f64, f64) = self
.state
.iter()
.fold((0.0, 0.0), |(s, c), &th| (s + th.sin(), c + th.cos()));
self.order_param = (sin_sum.powi(2) + cos_sum.powi(2)).sqrt() / self.n as f64;
let ds: f64 = self
.state
.iter()
.zip(prev.iter())
.map(|(a, b)| (a - b).powi(2))
.sum::<f64>()
.sqrt();
self.speed = ds / dt;
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::systems::DynamicalSystem;
#[test]
fn test_kuramoto_initial_state() {
let sys = Kuramoto::new(8, 2.0);
let s = sys.state();
assert_eq!(s.len(), 8);
assert_eq!(sys.dimension(), 8);
assert_eq!(sys.name(), "Kuramoto");
assert!(s.iter().all(|v| v.is_finite()));
for &th in s.iter() {
assert!(th >= 0.0 && th < std::f64::consts::TAU, "Phase out of range: {}", th);
}
}
#[test]
fn test_kuramoto_step_changes_state() {
let mut sys = Kuramoto::new(8, 2.0);
let before: Vec<f64> = sys.state().to_vec();
sys.step(0.01);
assert!(before.iter().zip(sys.state().iter()).any(|(a, b)| (a - b).abs() > 1e-15));
}
#[test]
fn test_kuramoto_phases_stay_wrapped() {
let mut sys = Kuramoto::new(8, 2.0);
for _ in 0..1000 {
sys.step(0.01);
}
for &th in sys.state().iter() {
assert!(th >= 0.0 && th < std::f64::consts::TAU, "Phase unwrapped: {}", th);
}
}
#[test]
fn test_kuramoto_order_parameter_in_range() {
let mut sys = Kuramoto::new(8, 2.0);
for _ in 0..500 {
sys.step(0.01);
}
let r = sys.order_parameter();
assert!(r >= 0.0 && r <= 1.0 + 1e-10, "Order param out of [0,1]: {}", r);
}
#[test]
fn test_kuramoto_set_state() {
let mut sys = Kuramoto::new(4, 2.0);
let new_phases = vec![0.1, 0.5, 1.0, 1.5];
sys.set_state(&new_phases);
for (i, &v) in sys.state().iter().enumerate() {
assert!((v - new_phases[i]).abs() < 1e-15, "set_state failed at i={}", i);
}
}
#[test]
fn test_kuramoto_high_coupling_synchronizes() {
let mut sys = Kuramoto::new(8, 20.0);
for _ in 0..2000 {
sys.step(0.01);
}
let r = sys.order_parameter();
assert!(r > 0.8, "High coupling should synchronize: r={}", r);
}
#[test]
fn test_kuramoto_speed_positive_after_step() {
let mut sys = Kuramoto::new(4, 0.5);
sys.step(0.01);
assert!(sys.speed() > 0.0, "speed should be positive after step: {}", sys.speed());
}
}