# -*- coding: utf-8 -*-
"""
Created on 18th May 2020

@author: Gauthier Legrand et Francis Pagaud ENS de Lyon
gauthier.legrand@ens-lyon.fr
francis.pagaud@ens-lyon.fr
"""


import numpy as np
import scipy.optimize as spo
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons
import matplotlib
matplotlib.rc('xtick', labelsize=24)
matplotlib.rc('ytick', labelsize=24)
matplotlib.rcParams.update({'font.size': 22})

###
#On part dune phase organique (volume V_org) dont on veut retirer une espèce. On dispose d'un volume total V_aq_tot d'eau et la constante de partage de l'espèce a extraire est K = C_aq/C_org

K = 10 #constante de partage
V_org = 1 #volume de la phase orga
V_aq_tot = 1 #volume total d'eau disponible
n = np.linspace(1,5, 5) # nombre de lavages

fig, ax = plt.subplots()
plt.subplots_adjust(left=0.15, bottom=0.25)


def lav(K, v_org, v_aq, n) :
    return(1 /(1+K*(v_aq / (n*v_org))))**n


ax1 = plt.subplot(111)

f, = plt.plot(n, lav(K, V_org, V_aq_tot, n), 'd', markersize = 20, lw=200)
plt.grid()
ax.margins(x=0.01)
plt.xticks(n)
plt.title('$ C_{org}/C_{org}^0=  {1}/{(1+ K {V_{aq}^{tot}}/{n V_{org}})^n} $',fontsize=30)
plt.ylabel('$ C_{org}/C_{org}^0 $', fontsize = 28)
plt.xlabel('$n$', fontsize = 28)


axK = plt.axes([0.15, 0.08, 0.6, 0.03])
axV_aq = plt.axes([0.15, 0.03, 0.6, 0.03])

sK = Slider(axK, '$K = C_{org}/C_{aq}$', 0.1, 100, valinit=10, valfmt='%0.2f')
sV_aq = Slider(axV_aq, '$V_{aq}^{tot}/V_{org}$', 0.1, 10, valinit=V_aq_tot, valfmt='%0.2f')


def update(val):
    v_aq = sV_aq.val
    k = sK.val
    f.set_ydata(lav(k, V_org, v_aq, n))
    fig.canvas.draw_idle()


sK.on_changed(update)
sV_aq.on_changed(update)


resetax = plt.axes([0.85, 0.03, 0.1, 0.04])
button = Button(resetax, 'Reset',  hovercolor='0.975')
axBout = plt.axes([0.85, 0.08, 0.1, 0.04])
Breset = Button(axBout, 'Rescale')

def reset(event):
    sK.reset()
    sV_aq.reset()
button.on_clicked(reset)


def rescale(event):
    ax1.relim()
    ax1.autoscale_view()
Breset.on_clicked(rescale)


plt.legend(framealpha = 0.5)

mng = plt.get_current_fig_manager()     #Plein ecran
mng.window.showMaximized()
plt.show()
