Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.61 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # Flore Nabet et Thomas Wick
  5. # Ecole Polytechnique
  6. # MAP 411 de Grégoire Allaire
  7. # Hiver 2016/2017
  8.  
  9. # Execution of *.py files
  10. # Possiblity 1: in terminal
  11. #  terminal> python3 file.py  # here file.py = convection.py
  12.  
  13. # Possiblity 2: executing in an python environment
  14. # such as spyder.
  15.  
  16. # Resolution numerique de l'equation de la chaleur
  17. # d_t u - u''(x) = 0
  18.  
  19. ########################################################
  20. # Load packages (need to be installed first if
  21. # not yet done - but is not difficult)
  22. import numpy as np
  23. import matplotlib.pyplot as plt # pour plot functons
  24. plt.switch_backend('tkAgg')  # necessary for OS SUSE 13.1 version,
  25. # otherwise, the plt.show() function will not display any window
  26.  
  27. import pylab
  28. import scipy as sp
  29. from scipy import sparse
  30. from scipy.sparse import linalg
  31.  
  32.  
  33. ########################################################
  34. # Parametres du probleme
  35. lg = 10        # intervalle en x=[-lg,lg]
  36. dx = 0.1       # dx = pas d'espace
  37. cfl = 0.55     # cfl = dt/dx^2
  38. dt = dx*dx*cfl # dt = pas de temps
  39. Tfinal = 0.5   # Temps final souhaite
  40.  
  41.  
  42. ########################################################
  43. # Initialisation
  44.  
  45. nx = 2*lg/dx + 1 # nx = nombre d'intervals
  46. x = np.linspace(-lg,lg,nx)
  47.  
  48. #print(int(nx))
  49.  
  50. # Initialize u0
  51. u0 = np.zeros(len(x))
  52. #print(len(u0))
  53.  
  54. # Set specific u0 values (same as in the scilab program)
  55. for k in range (len(x)):
  56.     if (1.0 - x[k]**2) < 0:
  57.        u0[k] = 0
  58.     else:
  59.        u0[k] = 1.0 - x[k]**2 # donnee initiale
  60.  
  61.  
  62. # Sanity check if everything works correctly
  63. #print (x)
  64. #print (u0)
  65.  
  66. # Plot initial condition
  67. plt.ion()
  68. plt.plot(x,u0,'o')
  69. #plt.show()
  70.  
  71. # We also write the intial condition into a file
  72. # of *.png or *.pdf format
  73. fig, ax = plt.subplots(nrows=1,ncols=1)
  74. ax.plot(x,u0) # trace u0 en fonction de x
  75. fig.savefig('u0.png')
  76. plt.close(fig)
  77.  
  78.  
  79. ########################################################
  80. # Schemas numeriques
  81.  
  82. # Initialize u by the initial data u0
  83. uimp = u0.copy() # il faut faire une copie sinon on va changer u0 en changeant u
  84. uexp = u0.copy()
  85.  
  86. # Construction de la matrice (creuse) pour le schema explicite
  87. # u(-10,t)=u(10,t)=0 on peut donc enlever ces deux coordonnees du systeme
  88. #u_j^{n+1}=nu*cfl*u_{j-1}^n+(1-2*nu*cfl)u_j^n+nu*cfl*u_{j+1}^n
  89. Aexp=sp.sparse.diags([cfl, 1-2*cfl, cfl], [-1, 0, 1], shape=(nx-2, nx-2))
  90.  
  91. # Construction de la matrice (creuse) pour le schema implicite
  92. #-nu*cfl*u_{j-1}^{n+1}+(1+2*nu*cfl)u_j^{n+1}-nu*cfl*u_{j+1}^{n+1}=u_j^n
  93. Aimp=sp.sparse.diags([-cfl, 1+2*cfl, -cfl], [-1, 0, 1], shape=(nx, nx))
  94.  
  95. # Sanity check to see how matrix looks like
  96. #print(Aexp.toarray())
  97. #print(Aimp.toarray())
  98.  
  99.  
  100. # Nombre de pas de temps effectues
  101. nt = int(Tfinal/dt)
  102. Tfinal = nt*dt # on corrige le temps final (si Tfinal/dt n'est pas entier)
  103.  
  104.  
  105. # Time loop
  106. for n in range(1,nt+1):
  107.  
  108.     # Solution 1: Solve explicit system
  109.     uexp[1:len(uexp)-1]=Aexp*uexp[1:len(uexp)-1]
  110.  
  111.     # Solution 2: schema implicite
  112.     uimp = sp.sparse.linalg.spsolve(Aimp,uimp)
  113.  
  114.  
  115.    # Print solution
  116.     if n%5 == 0:
  117.         plt.figure(2)
  118.         plt.clf()
  119.        
  120.         plt.subplot(121)
  121.         plt.plot(x,u0,'b',x,uexp,'r')
  122.         plt.xlabel('$x$')
  123.         plt.title('Schema explicite')
  124.        
  125.         plt.subplot(122)
  126.         plt.plot(x,u0,'b',x,uimp,'m')
  127.         plt.xlabel('$x$')
  128.         plt.title('Schema implicite')
  129.        
  130.         plt.pause(0.1)
  131.  #       plt.show()
  132.  
  133.         # Print solution into seperate files
  134. #        fig, ax = plt.subplots(nrows=1,ncols=1)
  135. #        ax.plot(x,uexp,'b',x,uimp,'r') # trace uexp et uimp en fonction de x
  136. #        # On peux utiliser *.png ou *.pdf
  137. #        fig.savefig(str(n) + 'u.png')
  138. #        plt.close(fig)
  139.  
  140. ####################################################################
  141. # comparaison solution exacte avec solution numerique au temps final
  142.  
  143. uexacte = np.zeros(len(u0))
  144.  
  145. def noyauc(x,t):
  146.     return np.exp(-x**2/(4*t))/np.sqrt(4*np.pi*t)
  147.  
  148. # on calcule la solution exacte en utilisant la formule des rectangles
  149. # uexacte(xi,Tfinal)=\int u0(y) noyauc(xi-y,Tfinal)
  150. # uexacte(xi,Tfinal)~sum_{j=0}^{2*lg/dx} dx*u0(xj) noyauc(xi-xj,Tfinal)
  151. # et xi=-lg+i*dx donc xi-xj=(i-j)*dx
  152. for i in range(int(nx)):
  153.     for j in range(int(nx)-1):
  154.         uexacte[i] = uexacte[i] + u0[j]*dx*noyauc((i-j)*dx,Tfinal)
  155.  
  156.  
  157. plt.figure(3)
  158. plt.clf()
  159. plt.plot(x,u0,'b',x,uexp,'or',x,uimp,'vm',x,uexacte,'k')
  160. plt.legend(['Donnee initiale','Schema explicite','Schema implicite','Solution exacte'],loc='best')
  161. plt.title('Comparaison entre la solution exacte et la solution numerique au temps final')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement