# Steady cellular solutions of the Kuramoto-Sivashinsky equation u_t + u u_x + u_xx + u_xxxx = 0,
# their linear (Floquet-Bloch) stability, and a check by direct simulation in a domain of length 100.
# Rob - Clint Sprott's AI assistant, November 7, 2026
import numpy as np
from scipy.optimize import root
M = 32                                       # number of harmonics in u = sum b_n sin(n k x)
n = np.arange(1, M + 1); th = np.arange(4*M)*2*np.pi/(4*M); S = np.sin(np.outer(th, n)); C = np.cos(np.outer(th, n))

def resid(b, k):                             # Fourier components of u u_x + u_xx + u_xxxx
    u = S @ b; r = u*(C @ (n*k*b)) + S @ (b*((n*k)**4 - (n*k)**2))
    return S.T @ r/(2*M)

def steady(k, b):
    return root(resid, b, args=(k,), method='lm', options={'xtol': 1e-14, 'ftol': 1e-14}).x

def growth(b, k):                            # largest growth rate of perturbations exp(iqx)*(period-2pi/k function)
    m = np.arange(-M, M + 1); uh = np.zeros(4*M + 1, complex); uh[2*M + n] = b/2j; uh[2*M - n] = -b/2j
    g = []
    for q in np.logspace(-4, np.log10(k/2), 40):
        kap = q + m*k
        A = np.diag(kap**2 - kap**4) - 1j*kap[:, None]*uh[2*M + m[:, None] - m[None, :]]
        g.append(np.linalg.eigvals(A).real.max())
    return max(g)

def simulate(u, L=100, dt=0.1, T=500):       # ETDRK4 (Kassam and Trefethen 2005)
    N = len(u); k = 2*np.pi*np.fft.rfftfreq(N, L/N); Lk = k**2 - k**4
    E, E2 = np.exp(dt*Lk), np.exp(dt*Lk/2); r = np.exp(1j*np.pi*(np.arange(1, 33) - 0.5)/32)
    LR = dt*Lk[:, None] + r; Q = dt*np.real(np.mean((np.exp(LR/2) - 1)/LR, 1))
    f1 = dt*np.real(np.mean((-4 - LR + np.exp(LR)*(4 - 3*LR + LR**2))/LR**3, 1))
    f2 = dt*np.real(np.mean((2 + LR + np.exp(LR)*(LR - 2))/LR**3, 1))
    f3 = dt*np.real(np.mean((-4 - 3*LR - LR**2 + np.exp(LR)*(4 - LR))/LR**3, 1))
    NL = lambda v: -0.5j*k*np.fft.rfft(np.fft.irfft(v, N)**2)
    v = np.fft.rfft(u)
    for i in range(int(T/dt)):
        Nv = NL(v); a = E2*v + Q*Nv; Na = NL(a); b = E2*v + Q*Na; Nb = NL(b); c = E2*a + Q*(2*Nb - Nv)
        v = E*v + Nv*f1 + 2*(Na + Nb)*f2 + NL(c)*f3
    return np.sqrt(np.mean(np.fft.irfft(Lk*v + NL(v), N)**2))   # rms of u_t (zero if steady)

b = np.zeros(M); b[0], b[1] = 0.955, -0.039                # weakly nonlinear guess near k = 1
cells = {}
for k in np.arange(0.99, 0.745, -0.005):                   # follow the branch down from k = 1
    b = steady(k, b); cells[round(k, 3)] = b
for k in (0.76, 0.765, 0.77, 0.8, 0.835, 0.84, 0.88, 0.94):
    kk = min(cells, key=lambda c: abs(c - k)); bk = steady(k, cells[kk])
    print("k = %.3f  b1 = %.4f  b2 = %.4f  b3 = %.4f  max growth = %+.1e" % (k, bk[0], bk[1], bk[2], growth(bk, k)))
x = 100*np.arange(256)/256; rng = np.random.default_rng(1)
for nc in (13, 14):
    k = 2*np.pi*nc/100; kk = min(cells, key=lambda c: abs(c - k)); bk = steady(k, cells[kk])
    u0 = np.sin(np.outer(k*x, n)) @ bk + 1e-3*rng.standard_normal(256)
    print("%d cells (k = %.4f): rms du/dt after t = 500 is %.1e" % (nc, k, simulate(u0)))
