sparsetrace commited on
Commit
b9bbb6e
·
verified ·
1 Parent(s): a6ddcdd

Update attractors.py

Browse files
Files changed (1) hide show
  1. attractors.py +98 -6
attractors.py CHANGED
@@ -80,7 +80,7 @@ def lorenz63(
80
  x0: Optional[Array] = None,
81
  seed: Optional[int] = 0,
82
  burn_in: int = 0,
83
- ) -> Array:
84
  """
85
  Lorenz-63 system (classic chaotic regime).
86
  Returns (T-burn_in, 3).
@@ -117,7 +117,7 @@ def lorenz96(
117
  x0: Optional[Array] = None,
118
  seed: Optional[int] = 0,
119
  burn_in: int = 0,
120
- ) -> Array:
121
  """
122
  Lorenz-96 in the standard chaotic regime (F ~ 8, dim ~ 40).
123
  Returns (T-burn_in, dim).
@@ -157,7 +157,7 @@ def rossler(
157
  x0: Optional[Array] = None,
158
  seed: Optional[int] = 0,
159
  burn_in: int = 0,
160
- ) -> Array:
161
  """
162
  Rössler attractor (classic chaotic parameters).
163
  Returns (T-burn_in, 3).
@@ -195,7 +195,7 @@ def henon(
195
  x0: Optional[Array] = None,
196
  seed: Optional[int] = 0,
197
  burn_in: int = 0,
198
- ) -> Array:
199
  """
200
  Hénon map (discrete-time chaos). dt is ignored.
201
  Returns (T-burn_in, 2).
@@ -229,7 +229,7 @@ def ikeda(
229
  x0: Optional[Array] = None,
230
  seed: Optional[int] = 0,
231
  burn_in: int = 0,
232
- ) -> Array:
233
  """
234
  Ikeda map (discrete-time chaos). dt is ignored.
235
  Standard real-form update:
@@ -272,7 +272,7 @@ def duffing(
272
  x0: Optional[Array] = None, # (x, v)
273
  seed: Optional[int] = 0,
274
  burn_in: int = 0,
275
- ) -> Array:
276
  """
277
  Forced Duffing oscillator in a commonly chaotic regime.
278
  State is (x, v) where v = x'.
@@ -334,3 +334,95 @@ def generate(name: str, T: int, dt: float = 0.01, **kwargs) -> Array:
334
  if key not in ATTRACTORS:
335
  raise KeyError(f"Unknown attractor '{name}'. Options: {sorted(ATTRACTORS.keys())}")
336
  return ATTRACTORS[key](T=T, dt=dt, **kwargs)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  x0: Optional[Array] = None,
81
  seed: Optional[int] = 0,
82
  burn_in: int = 0,
83
+ ) -> Array:
84
  """
85
  Lorenz-63 system (classic chaotic regime).
86
  Returns (T-burn_in, 3).
 
117
  x0: Optional[Array] = None,
118
  seed: Optional[int] = 0,
119
  burn_in: int = 0,
120
+ ) -> Array:
121
  """
122
  Lorenz-96 in the standard chaotic regime (F ~ 8, dim ~ 40).
123
  Returns (T-burn_in, dim).
 
157
  x0: Optional[Array] = None,
158
  seed: Optional[int] = 0,
159
  burn_in: int = 0,
160
+ ) -> Array:
161
  """
162
  Rössler attractor (classic chaotic parameters).
163
  Returns (T-burn_in, 3).
 
195
  x0: Optional[Array] = None,
196
  seed: Optional[int] = 0,
197
  burn_in: int = 0,
198
+ ) -> Array:
199
  """
200
  Hénon map (discrete-time chaos). dt is ignored.
201
  Returns (T-burn_in, 2).
 
229
  x0: Optional[Array] = None,
230
  seed: Optional[int] = 0,
231
  burn_in: int = 0,
232
+ ) -> Array:
233
  """
234
  Ikeda map (discrete-time chaos). dt is ignored.
235
  Standard real-form update:
 
272
  x0: Optional[Array] = None, # (x, v)
273
  seed: Optional[int] = 0,
274
  burn_in: int = 0,
275
+ ) -> Array:
276
  """
277
  Forced Duffing oscillator in a commonly chaotic regime.
278
  State is (x, v) where v = x'.
 
334
  if key not in ATTRACTORS:
335
  raise KeyError(f"Unknown attractor '{name}'. Options: {sorted(ATTRACTORS.keys())}")
336
  return ATTRACTORS[key](T=T, dt=dt, **kwargs)
337
+
338
+ def _chua_nonlinearity(x, m0, m1):
339
+ # h(x) = m1*x + 0.5*(m0-m1)(|x+1|-|x-1|)
340
+ return m1 * x + 0.5 * (m0 - m1) * (np.abs(x + 1.0) - np.abs(x - 1.0))
341
+
342
+ def chua(
343
+ T_total: int,
344
+ *,
345
+ dt: float = 0.01,
346
+ burn_in: int = 2000,
347
+ alpha: float = 15.6,
348
+ beta: float = 28.0,
349
+ m0: float = -1.15,
350
+ m1: float = -0.70,
351
+ seed: int = 0,
352
+ x0: np.ndarray | None = None,
353
+ ):
354
+ """
355
+ Chua double-scroll attractor.
356
+
357
+ Equations:
358
+ xdot = alpha*(y - x - h(x))
359
+ ydot = x - y + z
360
+ zdot = -beta*y
361
+ """
362
+ rng = np.random.default_rng(seed)
363
+
364
+ if x0 is None:
365
+ x = 0.1 * rng.standard_normal(3)
366
+ else:
367
+ x = np.array(x0, dtype=np.float64).reshape(3)
368
+
369
+ def f(x):
370
+ xx, yy, zz = x
371
+ h = _chua_nonlinearity(xx, m0=m0, m1=m1)
372
+ dx = alpha * (yy - xx - h)
373
+ dy = xx - yy + zz
374
+ dz = -beta * yy
375
+ return np.array([dx, dy, dz], dtype=np.float64)
376
+
377
+ # RK4
378
+ T = int(T_total)
379
+ out = np.zeros((T, 3), dtype=np.float64)
380
+ for n in range(T):
381
+ out[n] = x
382
+ k1 = f(x)
383
+ k2 = f(x + 0.5 * dt * k1)
384
+ k3 = f(x + 0.5 * dt * k2)
385
+ k4 = f(x + dt * k3)
386
+ x = x + (dt / 6.0) * (k1 + 2*k2 + 2*k3 + k4)
387
+
388
+ if burn_in > 0:
389
+ out = out[burn_in:]
390
+ return out
391
+
392
+ def torus_quasiperiodic(
393
+ T_total: int,
394
+ *,
395
+ dt: float = 0.01,
396
+ burn_in: int = 0,
397
+ R: float = 2.0,
398
+ r: float = 0.7,
399
+ omega1: float = 1.0,
400
+ omega2: float = np.sqrt(2.0), # irrational ratio -> quasi-periodic
401
+ seed: int = 0,
402
+ noise: float = 0.0, # set small (e.g. 1e-3) for "almost but not really"
403
+ ):
404
+ """
405
+ Quasiperiodic orbit on a torus embedded in R^3.
406
+
407
+ Returns:
408
+ (T_total - burn_in, 3)
409
+ """
410
+ rng = np.random.default_rng(seed)
411
+ T = int(T_total)
412
+ t = np.arange(T, dtype=np.float64) * float(dt)
413
+
414
+ th1 = omega1 * t
415
+ th2 = omega2 * t
416
+
417
+ x = (R + r * np.cos(th2)) * np.cos(th1)
418
+ y = (R + r * np.cos(th2)) * np.sin(th1)
419
+ z = r * np.sin(th2)
420
+
421
+ X = np.stack([x, y, z], axis=1)
422
+
423
+ if noise > 0:
424
+ X = X + noise * rng.standard_normal(size=X.shape)
425
+
426
+ if burn_in > 0:
427
+ X = X[burn_in:]
428
+ return X