Python and CFD--diffusion

原文鏈接:

diffusion?

nbviewer.jupyter.org

擴散現象的定義為:

Diffusion is the net movement of molecules or atoms from a region of high concentration (or high chemical potential) to a region of low concentration (or low chemical potential) as a result of random motion of the molecules or atoms. Diffusion is driven by a gradient in chemical potential of the diffusing species.

擴散作用源自於濃度差的存在,擴散現象將利於整個系統區域均勻。某點的擴散效應與該處的濃度梯度成正比,即為菲克定律。有必要指出,菲克定律並非嚴格成立的定律,如古典時期的大多數定律一樣,菲克定律僅僅是實際情況的線性近似,熱力學的分析將會告訴我們,菲克定律在理想粒子系統(理想氣體、理想溶液)的近平衡態輸運過程中是成立的

菲克定律微分形式表述為:

frac{partial u}{partial t}=
u frac{partial^2 u}{partial^2 t}\	ag{1}

使用泰勒展開進行有限差分離散擴散項:

u_{i+1}=u_{i}+left.Delta xfrac{partial u}{partial t}
ight|_{i}+left.frac{Delta x^2}{2}frac{partial u^2}{partial t^2}
ight|_{i}+left.frac{Delta x^3}{3!}frac{partial u^3}{partial t^3}
ight|_{i}+omicron (Delta x^4)\	ag{2}

u_{i-1}=u_{i}-left.Delta xfrac{partial u}{partial t}
ight|_{i}+left.frac{Delta x^2}{2}frac{partial u^2}{partial t^2}
ight|_{i}-left.frac{Delta x^3}{3!}frac{partial u^3}{partial t^3}
ight|_{i}+omicron (Delta x^4)\	ag{3}

將(2)、(3)相加,整理:

frac{partial u^2}{partial x^2}=frac{u_{i+1}-2u_i+u_{i-1}}{Delta x^2}+omicron({Delta x^2})\	ag{4}

最後在離散方程:

frac{u_i^{n+1}-u_i^n}{Delta t}=
ufrac{u_{i+1}^{n}-2u_i^{n}+u_{i-1}^{n}}{Delta x^2}\	ag{5}

整理得到:

u_i^{n+1}=u_i^n+frac{
u}{Delta t}frac{u_{i+1}^{n}-2u_i^{n}+u_{i-1}^{n}}{Delta x^2}\	ag{6}

最後使用python實現:

import numpy #loading our favorite libraryfrom matplotlib import pyplot #and the useful plotting library%matplotlib inlinenx = 41dx = 2 / (nx - 1)nt = 20 #the number of timesteps we want to calculatenu = 0.3 #the value of viscositysigma = .2 #sigma is a parameter, well learn more about it laterdt = sigma * dx**2 / nu #dt is defined using sigma ... more later!u = numpy.ones(nx) #a numpy array with nx elements all equal to 1.u[int(.5 / dx):int(1 / dx + 1)] = 2 #setting u = 2 between 0.5 and 1 as per our I.C.sun = numpy.ones(nx) #our placeholder array, un, to advance the solution in timefor n in range(nt): #iterate through time un = u.copy() ##copy the existing values of u into un for i in range(1, nx - 1): u[i] = un[i] + nu * dt / dx**2 * (un[i+1] - 2 * un[i] + un[i-1]) pyplot.plot(numpy.linspace(0, 2, nx), u);

本節在代碼上沒有難度,主要是理解擴散現象。

推薦閱讀:

優雅的 Python 之 Ellipsis
Django 學習小組:基於類的通用視圖詳解(一)
寫了一個小程序
新手向:十個趣味性Python腳本
數據分析入門必看案例:泰坦尼克號倖存率研究

TAG:Python | 計算流體力學CFD |