### Transition ferro/para ###

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import Slider


# Valeurs des paramètres : assez arbitraire...

T_C = 1000
a = 0.01
b = 0.1


# Potentiel :

def fstar(T_0, M):
    return  (a*(T_0 - T_C)*M*M/2 + b*M*M*M*M/4)

# Solution analytique pour M :
def Ms(T_0):
    if T_0>T_C :
        return 0
    else :
        return np.sqrt(a/b) * (T_C - T_0)**(1/2)


# Taille du texte :
s = 15

# Valeurs pour T_0 :
T_0max = 1300
T_0min = 200
LT_0 = np.linspace(T_0min, T_0max, 1000)

# Valeurs min et max de M :

Mmax = Ms(T_0min)
M = np.linspace(-2*Mmax, 2*Mmax, 1000)

def tracer(event) :


    f = fstar(sT.val, M)
    line1.set_data(M, f)
    pointeurs.set_data([-Ms(sT.val), Ms(sT.val)], [fstar(sT.val, Ms(sT.val)) , fstar(sT.val, Ms(sT.val))])
    ax.set_xlim(-2*Mmax, 2*Mmax)
    ax.set_ylim([-200, 300])
    ax.texts = []


    pointeurs2.set_data([sT.val, sT.val], [-Ms(sT.val) , Ms(sT.val)])

    ax2.set_xlim(T_0min, T_0max)
    ax2.set_ylim(-1.2*Mmax,1.2*Mmax)
    ax2.texts = []

    plt.draw()

fig = plt.figure()
ax = fig.add_subplot(121)
position = list(ax.get_position().bounds)

position[2] -= .01
position[1] +=0.05
ax.set_position(position)

ax.tick_params(axis = 'both', labelsize = s, pad = 5)
ax.set_title(r'Potentiel de Landau', size = s, pad = 3)
ax.set_xlabel(r'Aimantation M $(A/m)$', size = s)
ax.set_ylabel(r'Energie interne volumique $(J/m^3)$', size = s)
line1, = ax.plot([], [], linewidth = 3, color = [0,0.7,1], label = r'$f^* - f_0(T_0)$')
pointeurs, = ax.plot([], [], ':', linestyle = 'None', marker = '*', markersize = 10, color = (1, 0, 0))

ax.plot([-2*Mmax, 2*Mmax], [0,0], linestyle = '--', color = 'darkgrey', linewidth = 2)
ax.plot([0, 0], [-200,300], linestyle = '--', color = 'darkgrey', linewidth = 2)

plt.legend()


ax2 = fig.add_subplot(122)
position2 = list(ax2.get_position().bounds)

position2[0] += 0.05
position2[2] += .01
position2[1] += .05
ax2.set_position(position2)

ax2.tick_params(axis = 'both', labelsize = s,  pad = 5)
ax2.set_title(r'Aimantation', size = s, pad = 3)
ax2.set_xlabel(r'Température $T_0$ (K)', size = s)
ax2.set_ylabel(r'Aimantation $(A/m^3)$', size = s)
pointeurs2, = ax2.plot([], [], ':', linestyle = 'None', marker = '*', markersize = 10, color = (1, 0, 0))
ax2.plot([T_0min ,T_0max], [0, 0], linestyle = '--', color = 'darkgrey', linewidth = 2)
LMp = []
LMm = []
for T in LT_0:
    LMp.append(Ms(T))
    LMm.append(-Ms(T))
ax2.plot(LT_0, LMp, color = (.8, .2,0))
ax2.plot(LT_0, LMm,color = (.8, .2,0), label = r'$M^*$')
ax2.plot([-2*Mmax, 2*Mmax], [0,0], linestyle = '--', color = 'darkgrey', linewidth = 2)
ax2.plot([T_0min ,T_0max], [0, 0], linestyle = '--', color = 'darkgrey', linewidth = 2)
ax2.legend()

ax_T = fig.add_axes([.25, 0.05, .5, .01])
sT = Slider(ax_T,  r'$T \;(K)$', T_0min , T_0max, valinit = T_C, valfmt = '%i', orientation = 'horizontal', facecolor = 'red')
sT.on_changed(tracer)
sT.label.set_size(s)
sT.valtext.set_size(s)

tracer(2)
mng = plt.get_current_fig_manager()

plt.show()