duj626's picture
Release complete research artifacts and provenance
e1dc886 verified
Raw History Blame Contribute Delete
8.41 kB
"""Benchmark problem suite: mixed stiff / non-stiff ODE IVPs.
- Non-stiff: vdp5, bruss, lotka, kepler, rossler
- Stiff: vdp100, rober, orego, hires, pollu, bruss2d
- Reference solutions: agreement of Radau and BDF at rtol=atol=1e-13.
- Constants from canonical public sources: van der Pol (Hairer-Wanner), Robertson
(MATLAB ode15s demo), Oregonator (Hairer-Wanner/deTestSet), HIRES (Schafer),
POLLU (RODAS demo / MATLAB odesuite pollution), Brusselator 2D (method of lines).
"""
import numpy as np
PROBLEMS = {}
def reg(cls):
PROBLEMS[cls.name] = cls
return cls
class Problem:
name = "?"
t0, t1 = 0.0, 1.0
def f(self, t, y): raise NotImplementedError
def y0(self): raise NotImplementedError
# ---------------------------------------------------------------- non-stiff
@reg
class VdP5(Problem):
"""van der Pol oscillator, mu=5 (mild relaxation)."""
name = "vdp5"
t0, t1 = 0.0, 100.0
mu = 5.0
def y0(self): return np.array([2.0, 0.0])
def f(self, t, y):
return np.array([y[1], self.mu * (1 - y[0]**2) * y[1] - y[0]])
@reg
class Bruss(Problem):
"""Brusselator (2D, periodic orbit, non-stiff)."""
name = "bruss"
t0, t1 = 0.0, 30.0
def y0(self): return np.array([1.0, 3.0])
def f(self, t, y):
A = 1.0; B = 3.0
return np.array([A + y[0]**2 * y[1] - (B + 1) * y[0],
B * y[0] - y[0]**2 * y[1]])
@reg
class Lotka(Problem):
"""Lotka-Volterra predator-prey (periodic, non-stiff)."""
name = "lotka"
t0, t1 = 0.0, 20.0
def y0(self): return np.array([10.0, 5.0])
def f(self, t, y):
a, b, c, d = 1.5, 1.0, 3.0, 1.0
return np.array([a * y[0] - b * y[0] * y[1],
-c * y[1] + d * y[0] * y[1]])
@reg
class Kepler(Problem):
"""Two-body Kepler orbit, eccentricity 0.1, 10 orbits (periodic, non-stiff).
Eccentricity 0.1 (not 0.6): at e=0.6 the periapse passage drives global
error growth that no scipy solver keeps below ~3e-8 at rtol=1e-12, so a
1e-10 cross-solver reference is unattainable; e=0.1 restores the 1e-10
reference gate needed to judge the tightest scored tolerance (1e-8).
"""
name = "kepler"
t0, t1 = 0.0, 62.83185307179586 # 10 * 2*pi
def y0(self): return np.array([1 - 0.1, 0.0, 0.0, np.sqrt((1 + 0.1) / (1 - 0.1))])
def f(self, t, y):
r = np.hypot(y[0], y[1])
return np.array([y[2], y[3], -y[0] / r**3, -y[1] / r**3])
@reg
class Rossler(Problem):
"""Roessler attractor (chaotic, non-stiff).
Horizon t1=50 (not 200): chaotic global-error amplification (~e^{lambda*T},
lambda ~ 0.07) makes cross-solver reference agreement impossible beyond
~1e-10 at any fixed tolerance on long horizons. 50 keeps the Lyapunov
amplification ~30x so a 1e-12 reference stays ~3e-11.
"""
name = "rossler"
t0, t1 = 0.0, 50.0
def y0(self): return np.array([1.0, 1.0, 1.0])
def f(self, t, y):
a, b, c = 0.2, 0.2, 5.7
return np.array([-y[1] - y[2], y[0] + a * y[1], b + y[2] * (y[0] - c)])
# ---------------------------------------------------------------- stiff
@reg
class VdP100(Problem):
"""van der Pol oscillator, mu=100 (stiff relaxation oscillation)."""
name = "vdp100"
t0, t1 = 0.0, 1000.0
mu = 100.0
def y0(self): return np.array([2.0, 0.0])
def f(self, t, y):
return np.array([y[1], self.mu * (1 - y[0]**2) * y[1] - y[0]])
@reg
class Rober(Problem):
"""Robertson chemical kinetics (classic stiff; MATLAB ode15s demo, t in [0, 4e10])."""
name = "rober"
t0, t1 = 0.0, 4e10
def y0(self): return np.array([1.0, 0.0, 0.0])
def f(self, t, y):
k1, k2, k3 = 0.04, 3e7, 1e4
return np.array([-k1 * y[0] + k3 * y[1] * y[2],
k1 * y[0] - k3 * y[1] * y[2] - k2 * y[1]**2,
k2 * y[1]**2])
@reg
class Orego(Problem):
"""Oregonator (stiff chemical oscillator; Hairer-Wanner II)."""
name = "orego"
t0, t1 = 0.0, 360.0
def y0(self): return np.array([1.0, 2.0, 3.0])
def f(self, t, y):
c1, c2, c3 = 77.27, 8.375e-6, 0.161
a1 = y[0]; a2 = y[1]; a3 = y[2]
return np.array([c1 * (a2 + a1 * (1 - c1 * a1 - a2)),
(a3 - a2 * (1 + a1)) / c1,
c2 * (a1 - a3)])
@reg
class Hires(Problem):
"""HIRES plant physiology (stiff, 8D; Schafer, deTestSet)."""
name = "hires"
t0, t1 = 0.0, 321.8122
def y0(self):
return np.array([1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0057])
def f(self, t, y):
# Canonical HIRES (Schafer / MATLAB hires demo / Hairer-Wanner test set).
y1, y2, y3, y4, y5, y6, y7, y8 = y
return np.array([
-1.71 * y1 + 0.43 * y2 + 8.32 * y3 + 0.0007,
1.71 * y1 - 8.75 * y2,
-10.03 * y3 + 0.43 * y4 + 0.035 * y5,
8.32 * y2 + 1.71 * y3 - 1.12 * y4,
-1.745 * y5 + 0.43 * y6 + 0.43 * y7,
-280.0 * y6 * y8 + 0.69 * y4 + 1.71 * y5 - 0.43 * y6 + 0.69 * y7,
280.0 * y6 * y8 - 1.81 * y7,
-280.0 * y6 * y8 + 1.81 * y7,
])
@reg
class Pollu(Problem):
"""Air pollution model (stiff, 20D; RODAS demo / MATLAB odesuite 'pollution')."""
name = "pollu"
t0, t1 = 0.0, 60.0
def y0(self):
y = np.zeros(20)
y[1] = 0.2
return y
def f(self, t, y):
k1=.35; k2=.266e-1; k3=.123e5; k4=.86e-3; k5=.82e4
k6=.15e6; k7=.13e9; k8=.24e5; k9=.165e5; k10=.9e4
k11=.22e-1;k12=.12e7; k13=.188e1; k14=.163e5; k15=.48e7
k16=.35e-3;k17=.175e-1;k18=.1e9; k19=.444e12;k20=.124e4
k21=.21e1; k22=.578e1; k23=.474e-1;k24=.178e4
r = np.empty(20)
r[0] = -k1*y[0] - k10*y[0]*y[1] - k14*y[0]*y[6] - k23*y[0] - k24*y[0]*y[6]
r[1] = k1*y[0] - k10*y[0]*y[1] - k11*y[1]*y[2] - k12*y[1]*y[9] - k20*y[1]*y[11]
r[2] = k2*y[4] + k10*y[0]*y[1] + k11*y[1]*y[2] + k12*y[1]*y[9] - k4*y[2] - k13*y[2] - k20*y[2]*y[11]
r[3] = k3*y[1] - k2*y[4] + k9*y[7] + k21*y[10] - k17*y[3]
r[4] = k10*y[0]*y[1] + k12*y[1]*y[9] + k14*y[0]*y[6] + k23*y[0] + k24*y[0]*y[6]
r[5] = k4*y[2] - k5*y[6]*y[7] - k14*y[0]*y[6] - k15*y[5]*y[6] + k17*y[3] - k24*y[0]*y[6]
r[6] = k5*y[6]*y[7] - k6*y[7] - k7*y[7]*y[8] - k9*y[7] + k17*y[3]
r[7] = k5*y[6]*y[7] + k6*y[7] - k7*y[7]*y[8]
r[8] = k6*y[7] + k7*y[7]*y[8] + k8*y[5] + k9*y[7] + k10*y[0]*y[1] + k11*y[1]*y[2] \
+ k12*y[1]*y[9] + k16*y[12]*y[13] + k21*y[10]
r[9] = k9*y[7] + k21*y[10] + k22*y[11]
r[10] = k12*y[1]*y[9] - k20*y[1]*y[11] + k22*y[11]
r[11] = k8*y[5] - k16*y[12]*y[13] + k21*y[10]
r[12] = k13*y[2] - k16*y[12]*y[13] + k22*y[11]
r[13] = k15*y[5]*y[6] + k16*y[12]*y[13]
r[14] = k10*y[0]*y[1] + k11*y[1]*y[2] + k12*y[1]*y[9] - k18*y[14]
r[15] = k4*y[2] + k7*y[7]*y[8] + k13*y[2] + k14*y[0]*y[6] + k15*y[5]*y[6] \
+ k16*y[12]*y[13] + k18*y[14] - k19*y[15]
r[16] = k17*y[3] - k21*y[10] - k22*y[11] - k19*y[15]
r[17] = k18*y[14]
r[18] = k19*y[15]
r[19] = k20*y[1]*y[11] + k20*y[2]*y[11] + k22*y[11]
return r
@reg
class Bruss2d(Problem):
"""Brusselator 2D reaction-diffusion, method of lines, N x N grid.
u_t = 1 + u^2 v - 4.4 u + alpha*lap(u); v_t = 3.4 u - u^2 v + alpha*lap(v)
alpha = 0.02, periodic BC, N=14 -> 392 state variables.
"""
name = "bruss2d"
t0, t1 = 0.0, 10.0
N = 14
alpha = 0.02
def y0(self):
n = self.N
x = np.linspace(0, 1, n, endpoint=False)
X, Y = np.meshgrid(x, x)
u0 = 22.0 * Y * (1 - Y) ** 1.5
v0 = 27.0 * X * (1 - X) ** 1.5
return np.concatenate([u0.ravel(), v0.ravel()])
def f(self, t, y):
n = self.N
u = y[:n*n].reshape(n, n)
v = y[n*n:].reshape(n, n)
def lap(w): # 5-pt Laplacian, periodic
return (np.roll(w, 1, 0) + np.roll(w, -1, 0) + np.roll(w, 1, 1) + np.roll(w, -1, 1) - 4 * w)
du = 1.0 + u**2 * v - 4.4 * u + self.alpha * lap(u)
dv = 3.4 * u - u**2 * v + self.alpha * lap(v)
return np.concatenate([du.ravel(), dv.ravel()])
if __name__ == "__main__":
for name, cls in PROBLEMS.items():
p = cls()
y0 = p.y0()
print(f"{name:8s} dim={len(y0):4d} t=[{p.t0},{p.t1}]")