// TypeScript usage example
import {printE5} from 'diff-grok';
printE5();
# Python reference calculation for comparison
import numpy as np
import pandas as pd
from scipy.integrate import solve_ivp
# -------------------------
# 0. Solver method
# -------------------------
METHOD = "Radau" # stiff solver recommended
# -------------------------
# 1. Define E5 constants
# -------------------------
K1 = 7.89e-10
K2 = 1.13e9
K3 = 1.1e7
K4 = 1.13e3
# -------------------------
# 2. Define E5 ODE
# -------------------------
def e5(t, y):
y1, y2, y3, y4 = y
dy = np.zeros(4)
dy[0] = -K1 * y1 - K3 * y1 * y3
dy[1] = K1 * y1 - K2 * y2 * y3
dy[2] = K1 * y1 - K2 * y2 * y3 - K3 * y1 * y3 + K4 * y4
dy[3] = K3 * y1 * y3 - K4 * y4
return dy
# -------------------------
# 3. Initial conditions and time
# -------------------------
y0 = [0.00176, 0, 0, 0]
t_start = 0.0
t_end = 1e6
t_step = 2.5e3
# Ensure t_eval is within t_span
n_points = int(np.ceil((t_end - t_start) / t_step)) + 1
t_eval = np.linspace(t_start, t_end, n_points)
# -------------------------
# 4. Solve ODE
# -------------------------
sol = solve_ivp(
e5,
(t_start, t_end),
y0,
method=METHOD,
t_eval=t_eval,
rtol=1e-6,
atol=1e-6
)
print("Success:", sol.success)
print("Message:", sol.message)
# -------------------------
# 5. Save solution to CSV
# -------------------------
data = np.column_stack((sol.t, sol.y.T))
col_names = ['t', 'y1', 'y2', 'y3', 'y4']
df = pd.DataFrame(data, columns=col_names)
filename = f"E5-using-{METHOD}.csv"
df.to_csv(filename, index=False)
print(f"Solution saved to '{filename}'")
Print solution of the E5 benchmark problem