Тёмный

Solving the Heat Diffusion Equation (1D PDE) in Python 

Kody Powell
Подписаться 10 тыс.
Просмотров 73 тыс.
50% 1

Опубликовано:

 

24 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 53   
@BelmanCinematography
@BelmanCinematography 5 лет назад
import numpy as np import matplotlib.pyplot as plt L = 0.1 #Grosor de la pared en metros n = 10 #Numero de nodos utilizados T0 = 20 #Temperatura inicial T1s = 40 #Temperatura superficial en cara 1 T2s = 20 #Temperatura superficial en cara 2 dx = L/n alpha = 0.000000054713 #Difusividad termica K/(Rho*C_p) t_final = 1800 #Tiempo final en segundos dt = 60 x = np.linspace(dx/2, L-dx/2, n) T = np.ones(n)*T0 dTdt = np.empty(n) t = np.arange(0, t_final, dt) for j in range(1,len(t)): plt.clf() for i in range(1, n-1): dTdt[i] = alpha*(-(T[i]-T[i-1])/dx**2+(T[i+1]-T[i])/dx**2) dTdt[0] = alpha*(-(T[0]-T1s)/dx**2+(T[1]-T[0])/dx**2) dTdt[n-1] = alpha*(-(T[n-1]-T[n-2])/dx**2+(T2s-T[n-1])/dx**2) T = T + dTdt*dt plt.figure(1) plt.plot(x,T) plt.axis([0, L, 0, 50]) plt.xlabel('Distance (m)') plt.ylabel('Temperature (C)') plt.show() plt.pause(0.01)
@vsl8902
@vsl8902 4 года назад
I'm looking for a numerical code of Fourier equation. RU-vid algorithm bring me here. This is amazing! Thanks
@lauraburgstaller8682
@lauraburgstaller8682 4 года назад
do you know, how to save this animation as .mp4?
@riyazhudda3772
@riyazhudda3772 3 года назад
Excellent explanation. Thank you! I would replace all the dT/dt calculations as follows: # define this once discrete_calc = lambda Ti_1,Ti,Ti1 : alpha * (-(Ti-Ti_1)/dx**2 + (Ti1-Ti)/dx**2) Now replace the definition of dT/dt vector everywhere dTdt[i] = discrete_calc(T[i-1],T[i],T[i+1]) dtDt[0] = discrete_calc(T1s, T[0], T[1]) dtDt[n-1] = discrete_calc(T[n-2],T[n-1],T2s)
@manuelo1738
@manuelo1738 6 лет назад
Hello Mr. Powell, I would like to ask you a question. One the boundary conditions shound't the distance be dx/2? Also, on the first for loop the end should be len(t)+1 to finish in the last time step. Kind regards
@boudzzzzsssdddfffg
@boudzzzzsssdddfffg 4 года назад
For those struggeling with getting the animation right, it might help to add the command plt.ion() in the first for loop. That should stop it from opening the tab every run.
@sayanjitb
@sayanjitb 3 года назад
Yes, it just activates an interactive environment.
@PRACHISINGH-en4yz
@PRACHISINGH-en4yz 3 года назад
Thank you so much. Your comment saved me!
@sidebo1
@sidebo1 3 года назад
This is a great video, with one small "insidious" bug (meaning it's a bug that introduces an error, but won't cause program to crash, and in this case, would not introduce much error). It's with the 2 boundary conditions, where the distance from the node to the surface should be dx/2, not dx. It's an easy fix in those 2 lines of code.
@PRACHISINGH-en4yz
@PRACHISINGH-en4yz 3 года назад
Can you please let me know at what line, this change needs to be done?
@JamesVestal-dz5qm
@JamesVestal-dz5qm 6 месяцев назад
The heat transfer equation is from advanced transport phenomena 2, and were coding in python in reaction engineering. Brandon Tatum messaged me what does chemical engineering have to do with my presidential campaign and honestly i dont know. Brandon, my goal is to get healthy as a chemical engineer. Im learning about chemical engineering while waiting for my presidency to happen.
@biopellet
@biopellet Месяц назад
really helpful stuff
@gluijk
@gluijk 3 года назад
Very didactic example. It can be easily replicated in R using vector notation (hyper fast update since the 'for i' spatial loop becomes unnecesary): T=matrix(T0, nrow=1, ncol=n) T[1]=Tleft T[n]=Tright indices=which(col(T)!=1 & col(T)!=n) x=seq(from=dx/2, to=L-dx/2, length.out=n) # position array val=alpha*(dt/dx^2) for (j in 0:N) { plot(x, T, type='l', col='red', lwd=2, xlim=c(0,L), ylim=c(min(Tleft,Tright), max(Tleft,Tright)), ylab='T (ºC)', main=paste0('Iteration: ', j, '/',N, ', t=', j*dt, 's')) abline(v=c(x[1], x[n]), lty=2) abline(h=0) # Iterate T in vector notation T[indices]=T[indices]+val*(T[indices+1] - 2*T[indices] + T[indices-1]) }
@m35926
@m35926 5 лет назад
Quick question, what numerical method technique should I use if I want to find heat flow through a series of flat plates? So imagine we take your picture and keep the heat flow the same direction but push the wall on it's side so its layers are stacked up like pancakes. Any help with that? And awesome video.
@TriThom50
@TriThom50 Год назад
Is there a way to extend this to be able to apply heat at different points along the x direction? Also, would this result still hold if this was a thin rod, meaning could we consider a thin rod using the 1D equation too? Because in one of the lectures I know you mentioned a wall.
@LateMax2359
@LateMax2359 3 года назад
I wrote my own (very similar) program independently before discovering your guide. I've found that both our programs have the same bug. This may be a conceptual error with how I understand the heat transfer equation, but as dx decreases, I would expect the simulation to get more accurate, and not do anything weird. But, as I lower the thickness of the material or increase the number of nodes I find that the numbers begin blasting off by several orders of magnitudes immediately. I'm not sure why this occurs, but I'm (marginally) happier knowing that this isn't a unique problem. My current method is by lowering the number of nodes as much as is reasonable, and if continuing to lower it would be unreasonable I assume that steady state heat transfer begins almost immediately. I'd be interested to know what your opinion on the issue is.
@bluesybluesko9922
@bluesybluesko9922 Год назад
I might be late to the party, but here it goes : you have a so called CFL condition that connects to both dx and dt, and that condition should be lower than one, otherwise you start getting "junk" results and the method becomes unstable
@LateMax2359
@LateMax2359 Год назад
@@bluesybluesko9922 yes, very late, though very much appreciated
@JustMoseyinAround
@JustMoseyinAround 2 года назад
Very well done sir. Thank you
@KV-gy2mr
@KV-gy2mr 3 года назад
This is so cool. Could you also show us how to do this by solving a system of equations using linalg i.e implicit finite difference method?
@aurelia8028
@aurelia8028 3 года назад
Yeah I'd like to see how that is done aswell. I couldn't quite wrap my head around the wikipedia article with matrices and all that jazz and how I'd code that
@sarazahoor3110
@sarazahoor3110 6 лет назад
I tried to run your code and it is giving me the following error, $python main.py File "main.py", line 25 dTdt [0] = alpha*(-(T[0]-T1s)/dx**2+(T[1]-T[0])/dx**2) ^ SyntaxError: invalid syntax
@sayanjitb
@sayanjitb 3 года назад
Can you post the whole program that you have written?
@nabeelkubba
@nabeelkubba 6 лет назад
I'm trying to run this, but the figure only shows one profile then stops, after I close it, it then shows the next profile in time, it is not flowing as a video! Any ideas what is going on? I am using Python 3.7 and attempting to run it from Visual Studio. Thanks!
@luchtbuks1
@luchtbuks1 4 года назад
Use plt.draw() instead of plt.show()
@Lonkines
@Lonkines Год назад
Thank you very much for the video!
@stefanvet8135
@stefanvet8135 6 лет назад
Nice and clear explanation
@AdityaRoyiseast
@AdityaRoyiseast 5 лет назад
hey, why did you use linspace in 14 and arange in 19? why does error pops when i interchange them?
@AnupumPant
@AnupumPant 6 лет назад
Amazing! Works like a charm. I added a gaussian source in the middle too. But can you explain how can I add a zero flux boundary condition.
@euyin77
@euyin77 4 года назад
dTdx = 0 at x = L
@camimartinez6963
@camimartinez6963 3 года назад
thank you i love you you saved my life
@ecarlson
@ecarlson 3 года назад
Fantastic tutorial!
@dshshajskj6143
@dshshajskj6143 11 месяцев назад
CAn you make one using fem?
@bryanvazquez6422
@bryanvazquez6422 6 лет назад
thank's a lot for this video!, i've got a question... how did you make to "animate" the profile temperature at 23:47?
@shivamm9882
@shivamm9882 6 лет назад
That's what he told in the video. The reason to put plt.pause was to reduce the speed of the graph generation.
@alip6001
@alip6001 6 лет назад
plt.show()
@gabry88chesterfield
@gabry88chesterfield 5 лет назад
plt.ion() before the for cycle!
@ShashwatSharan
@ShashwatSharan 4 года назад
@@gabry88chesterfield finally it's moving! thanks!
@BelmanCinematography
@BelmanCinematography 5 лет назад
Instead of an animation it gives me a list of images at the end, I'm using Python via Jupyter 5.5.0, does anyone know how to turn it into an animation?
@ariaalizadeh8067
@ariaalizadeh8067 5 лет назад
I had the same problem. I just had to bring "plt.show()" outside of the for loop!
@juandadamo
@juandadamo 4 года назад
jupyter.brynmawr.edu/services/public/dblank/jupyter.cs/Examples/Animations%20in%20Matplotlib.ipynb A working example.
@alip6001
@alip6001 6 лет назад
sorry can you explain more about T=T+dTdt*dt, because my phthon says, operands could not be broadcast together with shapes (600,) (10,)
@andriesesun3073
@andriesesun3073 5 лет назад
excuse me,I meet the same question. Could you tell how to solve this question?
@mariospapanicolaou4631
@mariospapanicolaou4631 4 года назад
THANKS A LOT FRIEND!!!!
@laraibquamar7156
@laraibquamar7156 4 года назад
sir could you please explain ADI scheme for 2D heat equation in python.
@joaopedronevesgoldenstein8566
@joaopedronevesgoldenstein8566 3 года назад
Did you get it Laraib? I need it too
Далее
Solving the Heat Diffusion Equation (1D PDE) in Matlab
24:39
Solving PDEs with the FFT [Python]
14:56
Просмотров 51 тыс.
진 (Jin) 'I'll Be There' Official MV
03:15
Просмотров 3,5 млн
Cool Wrap! My Book is OUT 🥳
00:27
Просмотров 2,4 млн
Chemical Reaction Differential Equations in Python
20:34
How to Improve Blender's UI
20:27
Просмотров 14 тыс.
진 (Jin) 'I'll Be There' Official MV
03:15
Просмотров 3,5 млн