1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use super::{rk4, DynamicalSystem};
/// Rossler attractor (Rossler 1976) -- a three-dimensional spiral strange attractor.
///
/// Equations of motion:
///
/// dx/dt = -y - z
/// dy/dt = x + a*y
/// dz/dt = b + z*(x - c)
///
/// With a=0.2, b=0.2, c=5.7 the system exhibits a near-periodic orbit with
/// an approximate period of 5.9 time units. Increasing `c` causes
/// period-doubling bifurcations that lead to full chaos.
/// Integration uses fourth-order Runge-Kutta (RK4).
pub struct Rossler {
state: Vec<f64>,
pub a: f64,
pub b: f64,
pub c: f64,
speed: f64,
}
impl Rossler {
/// Creates a new Rössler attractor with the given parameters and initial state `(1, 0, 0)`.
///
/// # Parameters
/// - `a`: Controls the y-feedback; increasing `a` toward ~0.398 leads to chaos.
/// - `b`: Additive constant in the z-equation; typically small (e.g. 0.2).
/// - `c`: Shifts the z-nullcline; chaos is robust for `c` around 5.7.
///
/// # Returns
/// A `Rossler` instance ready for integration.
pub fn new(a: f64, b: f64, c: f64) -> Self {
Self {
state: vec![1.0, 0.0, 0.0],
a,
b,
c,
speed: 0.0,
}
}
fn deriv(s: &[f64], a: f64, b: f64, c: f64) -> Vec<f64> {
vec![-s[1] - s[2], s[0] + a * s[1], b + s[2] * (s[0] - c)]
}
}
impl DynamicalSystem for Rossler {
fn state(&self) -> &[f64] {
&self.state
}
fn dimension(&self) -> usize {
3
}
fn name(&self) -> &str {
"Rössler"
}
fn speed(&self) -> f64 {
self.speed
}
fn deriv_at(&self, state: &[f64]) -> Vec<f64> {
Self::deriv(state, self.a, self.b, self.c)
}
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];
}
}
}
/// Advances the attractor state by one RK4 integration step.
///
/// # Parameters
/// - `dt`: Time step size in simulation units.
fn step(&mut self, dt: f64) {
let (a, b, c) = (self.a, self.b, self.c);
let prev = self.state.clone();
rk4(&mut self.state, dt, |s| Self::deriv(s, a, b, c));
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_rossler_step_changes_state() {
let mut sys = Rossler::new(0.2, 0.2, 5.7);
let before: Vec<f64> = sys.state().to_vec();
sys.step(0.001);
let after = sys.state();
assert!(
before
.iter()
.zip(after.iter())
.any(|(a, b)| (a - b).abs() > 1e-15),
"State did not change after step: {:?} -> {:?}",
before,
after
);
}
#[test]
fn test_rossler_set_state() {
let mut sys = Rossler::new(0.2, 0.2, 5.7);
sys.set_state(&[2.0, 3.0, 4.0]);
let s = sys.state();
assert!((s[0] - 2.0).abs() < 1e-15);
assert!((s[1] - 3.0).abs() < 1e-15);
assert!((s[2] - 4.0).abs() < 1e-15);
}
#[test]
fn test_rossler_set_state_ignores_nan() {
let mut sys = Rossler::new(0.2, 0.2, 5.7);
sys.set_state(&[f64::NAN, 3.0, 4.0]);
let s = sys.state();
assert!((s[0] - 1.0).abs() < 1e-15, "NaN should not change state[0]");
assert!((s[1] - 3.0).abs() < 1e-15);
}
#[test]
fn test_rossler_period_positive_a() {
// With standard chaotic parameters, state stays finite and x oscillates in a
// bounded range after 1000 steps — the attractor is known to stay near [-15, 15].
let mut sys = Rossler::new(0.2, 0.2, 5.7);
for _ in 0..1000 {
sys.step(0.001);
}
let s = sys.state();
assert!(
s.iter().all(|v| v.is_finite()),
"State contains NaN/Inf: {:?}",
s
);
assert!(
s[0].abs() < 30.0 && s[1].abs() < 30.0,
"x/y out of expected bounds after 1000 steps: {:?}",
s
);
}
#[test]
fn test_rossler_deriv_at_known_point() {
// At (1, 0, 0) with a=0.2, b=0.2, c=5.7:
// dx = -0 - 0 = 0
// dy = 1 + 0.2*0 = 1
// dz = 0.2 + 0*(1 - 5.7) = 0.2
let sys = Rossler::new(0.2, 0.2, 5.7);
let d = sys.deriv_at(&[1.0, 0.0, 0.0]);
assert!((d[0] - 0.0).abs() < 1e-10, "dx should be 0: {}", d[0]);
assert!((d[1] - 1.0).abs() < 1e-10, "dy should be 1: {}", d[1]);
assert!((d[2] - 0.2).abs() < 1e-10, "dz should be 0.2: {}", d[2]);
}
#[test]
fn test_rossler_deterministic() {
let mut sys1 = Rossler::new(0.2, 0.2, 5.7);
let mut sys2 = Rossler::new(0.2, 0.2, 5.7);
for _ in 0..500 {
sys1.step(0.001);
sys2.step(0.001);
}
for (a, b) in sys1.state().iter().zip(sys2.state().iter()) {
assert!((a - b).abs() < 1e-15, "Non-deterministic: {} vs {}", a, b);
}
}
#[test]
fn test_rossler_speed_positive_after_step() {
let mut sys = Rossler::new(0.2, 0.2, 5.7);
sys.step(0.001);
assert!(sys.speed() > 0.0, "speed should be positive: {}", sys.speed());
}
}