import numpy as np
import matplotlib.pyplot as plt

import matplotlib as mpl
mpl.rcParams['font.family'] = 'serif'


lamb = 1.65 # W/m/K
cp =  1000 # J/kg/K
rho = 2150 # kg/3
e = 0.40 # m
D =  lamb/(rho*cp)  #  (à compléter) : coefficient de diffusion
tau = e**2/D

Tint = 293 # K
Text1 = 283 # K
Text2 = 263 # K

Nx = 50   # nombre de points
Nt = 10000   # nombre d'instants
tf = tau
Dx = e/(Nx-1)   #  (à compléter) : pas en espace
Dt = tf/(Nt-1)  #  (à compléter) : pas en temps

# initialisation du tableau de température
T = np.zeros((Nx,Nt)) # Nx lignes et Nt colonnes
for k in range(Nx): # CI (à t=0)
    T[k][0]=Tint + (Text1-Tint)/e*k*Dx
for i in range(Nt): # CAL (en x=0) et en x=e
    T[0][i]= Tint
    T[Nx-1][i]= Text2

r = D*Dt/Dx**2
for k in range(0,Nt-1):
    for i in range(1,Nx-1):
        T[i][k+1] = T[i][k] + r * (T[i-1][k] - 2*T[i][k] + T[i+1][k])



x=np.linspace(0,e,Nx)
t=np.linspace(0,tf,Nt)

## Evolution temporelle en différentes positions
plt.figure(1)
for i in [0,Nx//4,Nx//2,3*Nx//4,Nx-1]:
    plt.plot(t,T[i,:],label="x = {:.2f}m".format(i*Dx) )

plt.grid()
plt.xlabel("$t$ (s)")
plt.ylabel("$T$ (K)")
plt.legend()



plt.show()
plt.close()

## Evolution spatiale de T à différents instants
plt.figure(2)
for k in [0,Nt//100,Nt//30,Nt//10,Nt-1]:
    plt.plot(x,T[:,k],label="t = {:.0f}s".format(k*Dt) )



plt.grid()
plt.xlabel("$x$ (m)")
plt.ylabel("$T$ (K)")
plt.legend()



plt.show()



plt.close()