gpt4 book ai didi

python - 如何从 FiPy 中的 3D 变量中提取平面(3D 到 2D)

转载 作者:行者123 更新时间:2023-12-01 15:00:31 25 4
gpt4 key购买 nike

我在 3D 网格上有一个变量,我正在尝试切割一个平面图。我很惊讶以前没有人问过这个问题,它看起来是一个简单而常见的问题,但我还没有找到任何好的方法。如果有任何建议,我将不胜感激。


假设我有一个 3x3x5 平行六面体,我正在尝试提取一个 z 平面。

from fipy import *
from numpy import *
#Describes a 3x3x5 mesh
nx = 3
ny = 3
nz = 5

dx = 1
dy = 1
dz = 1

#Creates a 3D mesh and a 2D mesh to store the plane
mesh3D = Grid3D(dx, dy, dz, nx, ny, nz)
mesh2D = Grid2D(dx, dy, nx, ny)
#Defines the same variable, in 3D and in 2D
var2D = CellVariable(mesh = mesh2D)
var3D = CellVariable(mesh = mesh3D)

#Fills the 3D matrix
for i in xrange(nx*ny*nz*dx*dy*dz):
var3D[i] = i

print var3D

输出:

[  0.   1.   2.   3.   4.   5.   6.   7.   8.   9.  10.  11.  12.  13.  14.  15.  16.  17.  18.  19.  20.  21.  22.  23.  24.  25.  26.  27.  28.  29.  30.  31.  32.  33.  34.  35.  36.  37.  38.  39.  40.  41.  42.  43.  44.]

3D 变量看起来正确填充。

首先,我尝试使用此链接中描述的方式 http://permalink.gmane.org/gmane.comp.python.fipy/1576

The call method of a CellVariable enables interpolation to a set of coordinates passed in via the call method (the call method is just accessed using parentheses like in a function call). It returns a set of values for corresponding to each of the coordinates that were passed in. The order argument just determines the order of the interpolation.

我不确定这实际是如何工作的,但据我了解,这应该以 0 阶插入一个平面,因此它应该在特定平面上提取精确值。如果我错了,请纠正我。

x3D, y3D, z3D = mesh3D.getCellCenters()
x2D, y2D = mesh2D.getCellCenters()

for zcut in xrange(nz*dz):
var2D.setValue(var3D((x2D, y2D, zcut * ones(var2D.getMesh().getNumberOfCells())), order=0))
print "z-plane = %d" % zcut
print var2D

raw_input("Press any key to close")

奇怪的是它不起作用。偶数索引没问题,但奇数索引是连续平面的副本。

z-plane = 0
[ 0. 1. 2. 3. 4. 5. 6. 7. 8.]
z-plane = 1
[ 0. 1. 2. 3. 4. 5. 6. 7. 8.]
z-plane = 2
[ 18. 19. 20. 21. 22. 23. 24. 25. 26.]
z-plane = 3
[ 18. 19. 20. 21. 22. 23. 24. 25. 26.]
z-plane = 4
[ 36. 37. 38. 39. 40. 41. 42. 43. 44.]

我认为某处一定有一个愚蠢的错误,但我不知道。有什么想法吗?

最佳答案

单元格中心位于 z=0.5, 1.5, 2.5, ... 所以 FiPy 正在尽最大努力找到离 z=0, 1, 2, ... 最近的单元格

尝试

var2D.setValue(var3D((x2D, y2D, 
zcut * ones(var2D.mesh.numberOfCells) + dx/2.), order=0))

关于python - 如何从 FiPy 中的 3D 变量中提取平面(3D 到 2D),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29863958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com