"""
24/04/2020

Auteur : Francis Pagaud et Gauthier Legrand @ ENS lyon

Trace des courbes i-E pour mettre en evidence systeme rapide/lent et palier de diffusion.
"""

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import Slider, Button
import matplotlib
import scipy.special
import scipy
matplotlib.rc('xtick', labelsize=24)
matplotlib.rc('ytick', labelsize=24)
matplotlib.rcParams.update({'font.size': 22})

##On chope les courbes
#Les courbes présentant un plateau, on les approxime vite fait a la fonction erf. c'est juste visuel.
k0 = 0.11      #Parametre permettant de decaler les deux courbes erf, systeme lent ou rapide
i_palier1 = 0.02        #Courant de diffusion (mA)
i_palier2 = 0.02

E = np.arange(0.3, 1.2, 0.01) #On se place au potentiel Fe2+, centre autour de 0.77 V
C1 = (scipy.special.erf((E-0.77+k0)*12)-1)*i_palier1
C2 = (scipy.special.erf((E-0.77-k0)*12)+1)*i_palier2

Ctot = C1 + C2


##Figure


fig = plt.figure('Courbes i-E')
plt.clf()
axe_principal = plt.axes([0.15, 0.25, 0.75, 0.70])

axe_principal.plot([0.3, 1.2], [0, 0], '-k', linewidth = 2)
l, = axe_principal.plot(E, Ctot, '-b', linewidth = 4)
p, = axe_principal.plot(0.77, 0, 'or', markersize = 20, zorder=5)

axe_principal.set_xlabel('E (V)', fontsize = 28)
axe_principal.set_ylabel('i (A)', fontsize = 28)

axe_principal.text(0.74, -0.02, '$E_{Nernst}$', fontsize = 24, color = 'r')
axe_principal.text(0.4, -0.06, r'$\mathrm{Fe^{2+}}\leftarrow  \mathrm{Fe^{3+}}$', fontsize = 24)
axe_principal.text(1., 0.06, r'$\mathrm{Fe^{2+}}\rightarrow  \mathrm{Fe^{3+}}$', fontsize = 24)

axe_principal.axis([0.3, 1.2, -0.1, 0.1])
plt.grid()



resetax = plt.axes([0.025, 0.15, 0.06, 0.04])
button = Button(resetax, 'Reset', hovercolor='0.975')


axk = plt.axes([0.2, 0.025, 0.7, 0.03])	# Slider M, on fait varier le param k
axi2 = plt.axes([0.2, 0.065, 0.7, 0.03])	# Slider M, on fait varier le param i1
axi1 = plt.axes([0.2, 0.105, 0.7, 0.03])	# Slider M, on fait varier le param i2
sk = Slider(axk, '$\eta$ (V)', 0, 0.15, valinit=0, valfmt='%0.2f')
si2 = Slider(axi1, '$[\mathrm{Fe^{2+}}]$ (mol/L)', 0.0001, 0.12, valinit=0.05, valfmt='%0.2f')
si1 = Slider(axi2, '$[\mathrm{Fe^{3+}}]$ (mol/L)', 0.0001, 0.12, valinit=0.05, valfmt='%0.2f')


def update(val):
    k = (sk.val)+k0
    i_palier11 = si1.val/0.05*i_palier1
    i_palier22 = si2.val/0.05*i_palier2

    E_nernst = 0.77 + 0.059*np.log10(si1.val/si2.val)

    C11 = (scipy.special.erf((E-E_nernst+k)*12)-1)*i_palier11
    C22 = (scipy.special.erf((E-E_nernst-k)*12)+1)*i_palier22

    Ctot2 = C11 + C22

    l.set_ydata(Ctot2)
    p.set_xdata(E_nernst)

    fig.canvas.draw_idle()


sk.on_changed(update)
si1.on_changed(update)
si2.on_changed(update)

def reset(event):
    sk.reset()
    si1.reset()
    si2.reset()
button.on_clicked(reset)

mng = plt.get_current_fig_manager()     #Plein ecran
mng.window.showMaximized()
plt.show()

