Advertisement
Guest User

heat-equation-animation (DaHa)

a guest
Aug 20th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.87 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. L = 10                          # Length of rod
  5. nx = 100                        # number of points on the rod
  6. T = 500                         # Total time for simulation
  7. nt = 500                        # number of time steps
  8. alpha = 5e-3                    # Diffusion Coefficient
  9.  
  10. x = np.linspace(0,L, nx+1)      # points on the rod
  11. dx = x[1] - x[0]                # Spacing between points
  12. t = np.linspace(0,T, nt+1)      # instances in time
  13. dt = t[1] - t[0]                # time step
  14. F = alpha*dt/dx**2              # diffusion paramter
  15. fac = 1.0 - 2.0*F               # constant in the equation
  16.  
  17. # Set initial condition: a sine wave
  18. Temp_init = 100*np.sin(2*np.pi*x/L)
  19.  
  20. Temp_old = np.copy(Temp_init)   # known T at old time level
  21. Temp_new = np.zeros(nx+1)       # unknown T at the present time level
  22.  
  23. # To enable interactive plotting
  24. plt.ion()
  25.  
  26. for n in range(0, nt):
  27.     print('time t = ', t[n])
  28.     # Computre temperature at all the points
  29.     Temp_new[1:-1] = fac*Temp_old[1:-1] + F*(Temp_old[0:-2] + Temp_old[2:])
  30.  
  31.     # Update temperature before the next step
  32.     Temp_old[:] = Temp_new
  33.  
  34.     plt.figure(dpi = 150)
  35.     plt.clf()   # Clear the figure
  36.     plt.plot(x, Temp_init, color = 'red', label = 'Initial Temperature profile')
  37.     plt.plot(x, Temp_old, color = 'blue', label = 'Current Temperature') # Contour plotting the values
  38.     plt.xlabel('Length', fontsize = 10)     # X axis label
  39.     plt.ylabel('Temperature in $^o C$', fontsize = 10)      # Y axis label
  40.     plt.title('Temperature Diffusion in a 1D rod')      # Title
  41.     string = 'Time $t$ = '+str(t[n])+' s, $T_{max}$ = '+str(np.amax(Temp_old))+'$^o C$, $T_{min}$ = '+str(np.amin(Temp_old))+'$^o C$'
  42.     plt.suptitle(string)
  43.     plt.legend()
  44.     plt.grid(True)      # Enabling gridding
  45.     plt.axis((-0.1,10.1, -105,105))
  46.     plt.pause(0.01)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement