python - 给定速度场的 FiPy 对流
问题描述
我正在使用 FiPy 对给定速度场中的化学物质的对流/扩散进行建模,但我在将该场设置为 ConvectionTerm 系数时遇到了问题。
我的速度场由二维数组表示,假设Ugrid
水平速度和Vgrid
垂直速度,表示每个单元面的值。出于这个原因,我想说将此字段设置coeff
为 ConvectionTerm 参数的正确方法是将其分配给 FaceVariable。
但是,我不知道如何传递这两个数组作为value
FaceVariable。显然,我通过使用将字段设置为 CellVariable 没有问题,value = [np.ravel(Ugrid),np.ravel(Vgrid)]
并且对流模拟似乎也很有意义,但我认为这是不正确的,正如我上面简要提到的那样。
有什么建议么?
解决方案
FiPy 的一个重要考虑因素是网格索引不基于网格索引,因此从网格数组到单元格值的映射不应假定网格以任何特定方式排序。这使得有必要使用某种形式的插值。让我们首先构建一个带有网格索引的速度场,其中点位于网格点(而不是单元中心)上。
from fipy import Grid2D, CellVariable, FaceVariable
from fipy import ConvectionTerm, DiffusionTerm, TransientTerm
import numpy as np
from scipy import interpolate
dx = 0.5
nx = 7
dy = 0.5
ny = 7
xy = np.zeros((nx, ny, 2))
xy[:, :, 0] = np.mgrid[0:nx, 0:ny][0] * dx
xy[:, :, 1] = np.mgrid[0:nx, 0:ny][1] * dy
u = xy[..., 0] + xy[..., 1]
v = xy[..., 0] * xy[..., 1]
我们现在有一个(u, v)
速度场,每个形状nx, ny
和形状对应的坐标xy
, (nx, ny, 2)
。假设我们要将其插入到具有相同域但网格不同的 FiPy 网格中。
m = Grid2D(nx=3, ny=3, dx=1.0, dy=1.0)
网格不一定需要与速度场的网格点对齐。然后我们可以用
xy_interp = np.array(m.faceCenters).swapaxes(0, 1)
u_interp = interpolate.griddata(xy.reshape(-1, 2), u.flatten(), xy_interp, method='cubic')
v_interp = interpolate.griddata(xy.reshape(-1, 2), v.flatten(), xy_interp, method='cubic')
xy_interp
网格的面中心在哪里。请注意, usinggriddata
需要在xy_interp
内部,xy
否则它会给出 nan 值。一旦我们有了插值,我们就可以设置 FiPy 速度场。
velocity = FaceVariable(mesh=m, rank=1)
velocity[0, :] = u_interp
velocity[1, :] = v_interp
请注意, 的系数ConvectionTerm
可以是 aFaceVariable
或 a CellVariable
。一旦我们有了速度,我们就可以建立和求解方程了。
var = CellVariable(mesh=m)
eqn = (TransientTerm() + ConvectionTerm(velocity) == DiffusionTerm(1.0))
eqn.solve(var, dt=1.0)
这对我来说运行没有错误。
推荐阅读
- reactjs - 字符串长度调用函数
- python - 为什么我的 matplotlib 时间序列图如此混乱?
- javascript - 移动设备上的引导导航栏显示在其他所有内容的后面
- unity3d - 运行游戏时出现 Unity 故障
- android - 如何在primarySwatch小部件中为应用栏添加颜色,但颜色来自(fromARGB和fromRGBO)
- r - 在 R 中,如何让 update() 找到在调用 update() 的函数中创建的变量?
- node.js - 如何在 Linux 中的 Node.js 中并行启动 2 个程序
- performance - Perf 中的 CPU_CYCLES 是什么?
- ruby-on-rails-3 - 我如何使用 Active Query Methods 获得同样的结果?
- python - 如何加快 pandas.rolling weighted_average 或其他功能?