cinetique.py

Created by alex-juge84

Created on July 01, 2026

4.19 KB


from ion import *
from kandinsky import *
from math import log, exp


def ordre1(k):
    "Ordre 1 A->B: [A]=A0 exp(-kt), t1/2=ln2/k."
    print("ORDRE1 [A]=A0 exp(-kt)")
    print("t1/2=ln2/k=", round(log(2) / k, 6))
    print("t90%=ln10/k=", round(log(10) / k, 6))


def k_de_K(ks, K):
    "Equilibre A<=>B: donne k1,k-1 depuis ks=k1+k-1 et K=k1/k-1."
    km = ks / (1.0 + K); k1 = K * km
    print("k1=", k1)
    print("k-1=", km)
    print("K=", round(k1 / km, 4), "somme=", k1 + km)
    return k1, km


def K_eq(k1, km1):
    print("K=k1/k-1=", k1 / km1)
    return k1 / km1


def regression(ts, ys):
    n = len(ts); sx = sum(ts); sy = sum(ys)
    sxx = sum(t * t for t in ts); sxy = sum(ts[i] * ys[i] for i in range(n))
    a = (n * sxy - sx * sy) / (n * sxx - sx * sx); b = (sy - a * sx) / n
    my = sy / n
    st = sum((y - my) ** 2 for y in ys)
    se = sum((ys[i] - (a * ts[i] + b)) ** 2 for i in range(n))
    r2 = 1 - se / st if st > 0 else 1.0
    print("pente=", a, "ord=", b, "R2=", round(r2, 5))
    return a, b, r2


def relaxation(ts, vals, vinf, K=None):
    "Regresse ln(G-Ginf) vs t: pente=-(k1+k-1). G lineaire en c (alpha,absorbance...)."
    ys = []
    for g in vals:
        d = g - vinf
        if d <= 0:
            print("!! G-Ginf<=0, retire ce point"); return
        ys.append(log(d))
    a, b, r2 = regression(ts, ys)
    ks = -a
    print("k1+k-1=", ks)
    print("G0=", vinf + exp(b))
    if K is not None:
        k_de_K(ks, K)
    return ks


def paralleles(k1, k2):
    "2 voies // ordre 1: keff=k1+k2, selectivite=k1/k2."
    print("keff=k1+k2=", k1 + k2)
    print("select k1/k2=", round(k1 / k2, 4))
    print("t90%=", round(log(10) / (k1 + k2), 6))
    print("t1/2=", round(log(2) / (k1 + k2), 6))


def pseudo(k, exc):
    print("k_app=k*[exc]=", k * exc)


def pick(titre, opts):
    sel = 0
    while True:
        fill_rect(0, 0, 320, 222, color(255, 255, 255))
        draw_string(titre, 2, 2, color(255, 255, 255), color(60, 90, 200))
        for i in range(len(opts)):
            y = 26 + i * 20
            if i == sel:
                fill_rect(0, y, 320, 18, color(60, 120, 240))
                draw_string(opts[i], 6, y, color(255, 255, 255), color(60, 120, 240))
            else:
                draw_string(opts[i], 6, y)
        k = -1
        while k == -1:
            if keydown(KEY_UP):
                k = 0
            elif keydown(KEY_DOWN):
                k = 1
            elif keydown(KEY_BACK):
                k = 2
            elif keydown(KEY_OK) or keydown(KEY_EXE):
                k = 3
        while keydown(KEY_UP) or keydown(KEY_DOWN) or keydown(KEY_BACK) or keydown(KEY_OK) or keydown(KEY_EXE):
            pass
        if k == 0:
            sel = (sel - 1) % len(opts)
        elif k == 1:
            sel = (sel + 1) % len(opts)
        elif k == 2:
            return -1
        else:
            return sel


def _e(m):
    return float(input(m))


def menu():
    while True:
        c = pick("CINETIQUE", ["Ordre 1", "Equilibre k1,k-1", "Relaxation (donnees)", "Paralleles", "K (k1,k-1)", "Pseudo-ordre", "Console"])
        if c == -1 or c == 6:
            print("ex: k_de_K(2.6e-3,1.76)")
            return
        if c == 0:
            print("Q: ordre 1 simple, demi-reaction")
            ordre1(_e("k? "))
        elif c == 1:
            print("Q: trouver k1 et k-1 (on a K et la somme)")
            k_de_K(_e("k1+k-1? "), _e("K? "))
        elif c == 2:
            print("Q: k depuis un suivi (polarimetrie/absorbance)")
            print("G lineaire en c (alpha, absorbance, conductivite)")
            p = int(_e("Nb points? ")); ts = []; vs = []
            for i in range(p):
                ts.append(_e("t" + str(i + 1) + "? ")); vs.append(_e("  G? "))
            vinf = _e("Ginf? "); K = _e("K (0 si inconnu)? ")
            relaxation(ts, vs, vinf, K if K else None)
        elif c == 3:
            print("Q: 2 voies paralleles, selectivite")
            paralleles(_e("k1? "), _e("k2? "))
        elif c == 4:
            print("Q: constante d'equilibre K")
            K_eq(_e("k1? "), _e("k-1? "))
        elif c == 5:
            print("Q: reactif en exces, ordre apparent")
            pseudo(_e("k? "), _e("[exces]? "))
        input("--OK--")


menu()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.