# Chapitre 17 — Suivi cinétique par capteur de pression — CORRIGÉ
# (version exécutée pour produire images/ch17_suivi_pression.png)
R = 8.31
T = 298.15
V = 2.50e-4
n0 = 1.00e-2
P0 = 1013.0

t = [0, 100, 200, 300, 400, 500, 600, 700,
     800, 900, 1000, 1100, 1200]
P = [1013, 1117, 1195, 1262, 1310, 1353, 1385, 1409,
     1432, 1447, 1458, 1471, 1478]

n_O2 = []
n_rest = []
for i in range(len(t)):
    dP = (P[i] - P0) * 100
    nO2 = dP * V / (R * T)        # PV = nRT, applique au gaz forme   # COMPLÉTÉ
    nH2O2 = n0 - 2 * nO2          # 2 H2O2 consommes par O2 forme     # COMPLÉTÉ
    n_O2.append(nO2)
    n_rest.append(nH2O2)

i = 0
while n_rest[i] > n0 / 2:
    i = i + 1
print("temps de demi-reaction : environ", t[i], "s")
n_rest_mmol = []
for n in n_rest:
    n_rest_mmol.append(n * 1e3)
print("H2O2 restant (mmol) :", n_rest_mmol)

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
plt.figure(figsize=[7.2, 4.2], dpi=160)
plt.plot(t, n_rest_mmol, 'o-', color='#1f6f8b',
         label="H$_2$O$_2$ restant")
plt.axhline(n0 / 2 * 1e3, linestyle='--', color='gray',
            label="$n_0/2$")
plt.axvline(300, linestyle=':', color='#c0392b')
plt.annotate("$t_{1/2} \\simeq 300$ s", [310, 6.6], color='#c0392b',
             fontsize=11)
plt.xlabel("$t$ (s)")
plt.ylabel("$n$ (mmol)")
plt.title("Le capteur de pression suit la réaction : $P \\to n$ (dette du ch. 07 soldée)",
          fontsize=10)
plt.legend()
plt.grid(alpha=0.4)
plt.tight_layout()
plt.savefig("../images/ch17_suivi_pression.png")
print("figure ecrite : ../images/ch17_suivi_pression.png")
