擴散方程我感興趣的解決,使用FiPy和Mayavi的解決3D
\frac{\delta \phi}{\delta t} - D \nabla^2 \phi - \alpha \phi - \gamma \phi = 0
以下是工作,但我有幾個問題:
- 是否有可能通過FiPy提高性能?儘管計算時間很長,但我覺得這裏的
nx, ny, nz
箱子非常小。我不明白爲什麼陣列X, Y, and Z
是如此之大。 - 請注意,在第一幀中,我們放大了。如何強制範圍在所有地塊中自動爲
[0..nx, 0..ny, 0..nz]
? - 第一個幀的數據是一個由
0.0
包圍的值爲1.0
的點的球體。爲什麼似乎有一個漸變? Mayavi插值?如果是這樣,我該如何禁用它?
代碼:
from fipy import *
import mayavi.mlab as mlab
import numpy as np
import time
# Spatial parameters
nx = ny = nz = 30 # bins
dx = dy = dz = 1 # Must this be an integer?
L = nx * dx
# Diffusion and time step
D = 1.
dt = 10.0 * dx**2/(2. * D)
steps = 4
# Initial value and radius of concentration
phi0 = 1.0
r = 3.0
# Rates
alpha = 1.0 # Source coeficcient
gamma = .01 # Sink coeficcient
mesh = Grid3D(nx=nx, ny=ny, nz=nz, dx=dx, dy=dy, dz=dz)
X, Y, Z = mesh.cellCenters # These are large arrays
phi = CellVariable(mesh=mesh, name=r"$\phi$", value=0.)
src = phi * alpha # Source term (zeroth order reaction)
degr = -gamma * phi # Sink term (degredation)
eq = TransientTerm() == DiffusionTerm(D) + src + degr
# Initial concentration is a sphere located in the center of a bounded cube
phi.setValue(1.0, where=(((X-nx/2))**2 + (Y-ny/2)**2 + (Z-nz/2)**2 < r**2))
# Solve
start_time = time.time()
results = [phi.getNumericValue().copy()]
for step in range(steps):
eq.solve(var=phi, dt=dt)
results.append(phi.getNumericValue().copy())
print 'Time elapsed:', time.time() - start_time
# Plot
for i, res in enumerate(results):
fig = mlab.figure()
res = res.reshape(nx, ny, nz)
mlab.contour3d(res, opacity=.3, vmin=0, vmax=1, contours=100, transparent=True, extent=[0, 10, 0, 10, 0, 10])
mlab.colorbar()
mlab.savefig('diffusion3d_%i.png'%(i+1))
mlab.close()
經過時間:68.2秒