#!/bin/env python3

# from math import *
import matplotlib.pyplot as plt
import numpy as np
import scipy.optimize

"""
Date : 2019
Auteur : Joachim Galiana, Manon Leconte


Ce programme vise à tracer les branches d'un diagramme binaire liquide/vapeur possédant un point hétéroazéotropique.
Les deux branches sont calculées et tracées et leur intersection (hétéroazéotrope) est calculée et pointée de même.
"""

#DÉFINITION DES GRANDEURS et CONSTANTES PHYSIQUES
Teb_t=273+110.6#en K
Teb_e=273+100.0#en K
R=8.314#en J/K/mol
Delta_eb_t=3.86e4
Delta_eb_e=4.08e4
M_t=92.1
M_e=18.0
mtot=2.0

#DÉFINITION DES VARIABLES
x=np.linspace(0.01,0.99,99)
y=1-x

#DÉFINITION DES FONCTIONS
def Tt(fractions):
    return((Teb_t*Delta_eb_t)/(Delta_eb_t-R*Teb_t*np.log(fractions)))
def Te(fractions):
    return((Teb_e*Delta_eb_e)/(Delta_eb_e-R*Teb_e*np.log(fractions)))
def Soustraction(fractions):
    # return (Teb_t*Delta_eb_t)/(Delta_eb_t-R*Teb_t*np.log(fractions)) - (Teb_e*Delta_eb_e)/(Delta_eb_e-R*Teb_e*np.log(1-fractions))
    return (Te(fractions) - Tt(1-fractions))

# Tp=Tp(x)
# Td=Td(y)

"""
L'abcisse utilisée est x, fraction molaire en eau : c'est toujours par rapport au Durène qu'on réfléchit à partir d'ici.
"""

H=scipy.optimize.fsolve(Soustraction,0)
print(H)
ante_x=np.array([a for a in x if a < H])
post_x=np.array([a for a in x if a > H])

plt.ylim(340,400)
# plt.plot(x,Tt(x),"red")
plt.plot(ante_x,Te(ante_x),"red",linestyle='--')
plt.plot(post_x,Te(post_x),"red")
plt.plot(ante_x,Tt(1-ante_x),"blue")
plt.plot(post_x,Tt(1-post_x),"blue",linestyle='--')
# plt.plot(x,Tp(y))
plt.plot(H,Te(H),"+")
plt.hlines(y=Te(H),xmin=0,xmax=1)
# plt.hlines(x=E[0],ymin=0,ymax=Td(E)[0])
# plt.plot(x,Soustraction(x))
plt.xlabel("Fraction molaire en eau, x")
plt.ylabel("Température, T (K)")
plt.title("Diagramme binaire liquide/vapeur toluène-eau\n hétéroazéotrope à x = 0.58")
plt.grid()
plt.show()
