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)
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)
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
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.
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.
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.
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.
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.
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.
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
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
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
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!
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?