advencement.py

Created by thibaut-lyon

Created on September 23, 2020

2.03 KB


#pip install numpy et pip install matplotlib si non installés

import matplotlib.pyplot as plt

def courbes(a,n_Ai,b,n_Bi):

    x=0

    n_A=n_Ai

    n_B=n_Bi

    dx=(min(n_A,n_B)/100)

    plt.ion()

    x_max=min(n_Ai/a,n_Bi/b)

    plt.xlim(0,1.2*x_max)

    plt.ylim(0,1.2*max(n_A,n_B))

    plt.xlabel('Avancement x (mol)')

    plt.ylabel('n (en mol)')

    plt.grid()

    plt.plot(x,n_A,'b.',label='n(A)')

    plt.plot(x,n_B,'r.',label='n(B)')

    plt.legend()

    while (n_A>0) and (n_B>0):

        plt.plot(x,n_A,'b.')

        plt.plot(x,n_B,'r.')

        plt.pause(0.01)

        x=x+dx

        n_A=n_Ai-a*x

        n_B=n_Bi-b*x



def avancement_maximal(a,n_Ai,b,n_Bi):

    x_max=min(n_Ai/a, n_Bi/b)

    return x_max

def etat_final(a,n_Ai,b,n_Bi,x_max):

    n_A=n_Ai-a*x_max

    n_B=n_Bi-b*x_max

    if (n_A<1E-10 and n_B<1E-10):

        print('Nous sommes dans les conditions stoechiométriques')

        print('x_max = {0:1.2E} mol.'.format(x_max))

    elif n_A<1E-10 :

        n_A=0

        print('A est le réactif limitant.')

        print('x_max = {0:1.2E} mol.'.format(x_max))

        #print('A la fin de la transformation, il n'y a plus de A.')

        print('Il reste {0:1.2E} mol de B.'.format(n_B))

    else :

        n_B=0

        print('B est le réactif limitant.')

        print('x_max = {0:1.2E} mol.'.format(x_max))

        #print('A la fin de la transformation, il n'y a plus de B.')

        print('Il reste {0:1.2E} mol de A.'.format(n_A))

#Le programme principal---------------------------------------------

#Equation du type aA + bB -> ........

#print('Entrez le nombre stoechiométrique a :')

a=int(input('Entrez le nombre stoechiométrique a :'))

#print('Entrez la quantité initiale de A :')

n_Ai=float(input('Entrez la quantité initiale de A :'))

b=int(input('Entrez le nombre stoechiométrique b :'))

n_Bi=float(input('Entrez la quantité initiale de B '))

x_max=avancement_maximal(a,n_Ai,b,n_Bi)

etat_final(a,n_Ai,b,n_Bi,x_max)

courbes(a,n_Ai,b,n_Bi)

plt.ioff()

plt.show()