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

  Рет қаралды 74,293

Kody Powell

Kody Powell

Күн бұрын

Пікірлер: 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. KZbin 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)
@sidebo1
@sidebo1 4 жыл бұрын
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?
@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!
@JustMoseyinAround
@JustMoseyinAround 3 жыл бұрын
Very well done sir. Thank you
@stefanvet8135
@stefanvet8135 6 жыл бұрын
Nice and clear explanation
@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!
@biopellet
@biopellet 3 ай бұрын
really helpful stuff
@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
@TriThom50
@TriThom50 2 жыл бұрын
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.
@camimartinez6963
@camimartinez6963 3 жыл бұрын
thank you i love you you saved my life
@JamesVestal-dz5qm
@JamesVestal-dz5qm 8 ай бұрын
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.
@Lonkines
@Lonkines 2 жыл бұрын
Thank you very much for the video!
@m35926
@m35926 6 жыл бұрын
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.
@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?
@ecarlson
@ecarlson 3 жыл бұрын
Fantastic tutorial!
@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 2 жыл бұрын
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 2 жыл бұрын
@@bluesybluesko9922 yes, very late, though very much appreciated
@gluijk
@gluijk 4 жыл бұрын
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]) }
@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()
@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 5 жыл бұрын
dTdx = 0 at x = L
@AdityaRoyiseast
@AdityaRoyiseast 5 жыл бұрын
hey, why did you use linspace in 14 and arange in 19? why does error pops when i interchange them?
@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.
@dshshajskj6143
@dshshajskj6143 Жыл бұрын
CAn you make one using fem?
@mariospapanicolaou4631
@mariospapanicolaou4631 4 жыл бұрын
THANKS A LOT FRIEND!!!!
@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?
@laraibquamar7156
@laraibquamar7156 4 жыл бұрын
sir could you please explain ADI scheme for 2D heat equation in python.
@joaopedronevesgoldenstein8566
@joaopedronevesgoldenstein8566 4 жыл бұрын
Did you get it Laraib? I need it too
Solving the Heat Diffusion Equation (1D PDE) in Matlab
24:39
Kody Powell
Рет қаралды 137 М.
I'VE MADE A CUTE FLYING LOLLIPOP FOR MY KID #SHORTS
0:48
A Plus School
Рет қаралды 20 МЛН
UFC 287 : Перейра VS Адесанья 2
6:02
Setanta Sports UFC
Рет қаралды 486 М.
Ful Video ☝🏻☝🏻☝🏻
1:01
Arkeolog
Рет қаралды 14 МЛН
Andro, ELMAN, TONI, MONA - Зари (Official Music Video)
2:50
RAAVA MUSIC
Рет қаралды 2 МЛН
Solving Heat equation PDE using Explicit method in Python
15:38
Shameel Abdulla
Рет қаралды 49 М.
Solving Systems Of Equations Using Sympy And Numpy (Python)
15:23
Andrew Dotson
Рет қаралды 78 М.
Solving PDEs with the FFT [Python]
14:56
Steve Brunton
Рет қаралды 52 М.
Solving the Navier-Stokes equations in Python | CFD in Python | Lid-Driven Cavity
29:15
Machine Learning & Simulation
Рет қаралды 69 М.
Solve the heat equation PDE using the Implicit method in Python
24:11
Shameel Abdulla
Рет қаралды 25 М.
I'VE MADE A CUTE FLYING LOLLIPOP FOR MY KID #SHORTS
0:48
A Plus School
Рет қаралды 20 МЛН