# Simulate a simple kinetic model demonstrating synergy in a reaction A -> B
# where two catalysts X and Y each increase the rate, and together they add an
# extra interaction term (synergy). We'll compare conversions over time.

import numpy as np
import matplotlib.pyplot as plt

# Parameters
A0 = 1.0          # initial concentration of A (arbitrary units)
k_base = 0.02     # base first-order rate constant (1/min)
kx = 0.08         # rate boost per unit of X
ky = 0.05         # rate boost per unit of Y
kxy = 0.15        # additional synergy term when both are present

# Catalyst "loads" (dimensionless here, think of normalized amounts)
X = 1.0
Y = 1.0

# Time grid
t_end = 120.0   # minutes
dt = 0.2
t = np.arange(0, t_end + dt, dt)

def simulate(X_amt, Y_amt, include_synergy=True):
    # Effective first-order rate constant (toy model)
    k = k_base + kx*X_amt + ky*Y_amt
    if include_synergy and (X_amt>0 and Y_amt>0):
        k += kxy*X_amt*Y_amt
    # Analytical solution for first-order decay: A(t) = A0 * exp(-k t)
    A = A0 * np.exp(-k * t)
    B = A0 - A
    return A, B, k

# Scenarios
scenarios = {
    "No catalyst": (0.0, 0.0, True),
    "X only": (X, 0.0, True),
    "Y only": (0.0, Y, True),
    "X+Y (no synergy)": (X, Y, False),
    "X+Y (with synergy)": (X, Y, True),
}

results = {}
for name, (x_amt, y_amt, sy) in scenarios.items():
    A, B, k = simulate(x_amt, y_amt, include_synergy=sy)
    results[name] = {"A": A, "B": B, "k": k}

# Plot conversion of A -> B over time
plt.figure(figsize=(7,4.5))
for name in ["No catalyst", "X only", "Y only", "X+Y (no synergy)", "X+Y (with synergy)"]:
    plt.plot(t, results[name]["B"], label=f"{name}  (k={results[name]['k']:.2f})")
plt.xlabel("Time (min)")
plt.ylabel("Conversion to B")
plt.title("Synergy example in A → B reaction")
plt.legend(loc="lower right")
plt.tight_layout()
plt.show()

# Summarize final conversions at t_end
finals = {name: float(vals["B"][-1]) for name, vals in results.items()}
finals
