擴散方程
相當一部分的物理問題,特別是動力學問題,一個近似的表述是:給出了初始條件,未來會發生什麼?
擴散方程
where D is the diffusion constant. This equation is used to calculate the motion of diffusing gases and liquids, as well as the flow of heat in thermal conductors. It』s a PDE, and as we』ve expressed it there are two indepedent variables.
栗子
temperature profile of a container made of stainless steel, and submerged in two different baths
Also, let the container be 1cm deep in the outer cold water. The thermal conduction here is governed by the heat equation
So then we make a grid, and solve it, assigning D = 4.25×10?6m2s?1.
from numpy import emptynfrom pylab import plot , xlabel , ylabel , shownimport numpy as npn# Cons tant snL = 0.01 # Thicknes s o f s t e e l in meter snD = 4.25e-6 # Thermal diffusivitynN = 100 # Number of d i v i s i o n s in g r idna = L/N # Grid spac ingnh = 1e-4 # Time?s t epnepsilon = h/1000nTlo = 0.0 # Low temperature in Ce l c iusnTmid = 20.0 # Int e rmediat e temperature in Ce l c iusnThi = 50.0 # Hi temperature in Ce l c iusnt1 = 0.01nt2 = 0.1nt3 = 0.4nt4 = 1.0nt5 = 10.0ntend = t5 + epsilonn# Create arraysnT = empty (N+1, float )nT[0] = ThinT[N] = TlonT[1:N] = TmidnTp = empty (N+1, float )nTp [0] = ThinTp[N] = Tlon# Main loopnt = 0.0nc = h*D/( a*a )nwhile t<tend:n # Ca l cul a t e the new values of Tn for i in range (1 ,N) :n Tp[ i ] = T[ i ] + c *(T[ i+1]+T[ i -1]-2*T[ i ] )n T,Tp = Tp,Tn t += hn # Make p l o t s at the given t imesn if abs ( t-t1 )<epsilon :n plot (T)n if abs ( t-t2 )<epsilon :n plot (T)n if abs ( t-t3 )<epsilon :n plot (T)n n if abs ( t-t4 )<epsilon :n plot (T)n if abs ( t-t5 )<epsilon :n plot (T)n
Note that starting out, the profile is biased, but then it becomes more and more linear as time goes on. Also, we could also implement time-varying boundary conditions.
推薦閱讀:
※Python科學計算與自動控制1-Python入門
※使用VASPy快速處理VASP文件以及數據可視化
※和 C++ 相比,用 Fortran 編程是怎樣的體驗?
※Fortran 目前仍然是科學計算領域使用的主要語言嗎?
※寫kinetic Monte Carlo模擬程序用什麼語言合適?