如何从FiPy(3D到2D)中的3D变量中提取平面 [英] How to extract a plane from a 3D variable in FiPy (3D to 2D)
问题描述
我在3D网格上有一个变量,我正在尝试切割一个计划.我很惊讶以前没有问过这个问题,这看起来是一个简单而常见的问题,但是我还没有找到任何好的方法.我将不胜感激.
I have a variable on a 3D mesh and I am trying to cut a plan. I am surprised this question hasn't been asked before, it looks an easy and common problem but I haven't found any good way. I would appreciate any advice.
假设我有一个平行六面体3x3x5,并且我正在尝试提取z平面.
Let's say that I have a parallelepiped 3x3x5 and that I am trying to extract a z-plane.
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变量看起来正确填充.
The 3D variable looks correctly filled.
首先,我尝试使用此链接中所述的方法 http://permalink.gmane.org/gmane.comp.python.fipy/1576
First, I tried to use the way described at this link http://permalink.gmane.org/gmane.comp.python.fipy/1576
CellVariable允许对传入的一组坐标进行插值 通过 call 方法(只需使用以下方法访问 call 方法 括号,就像在函数调用中一样).它返回一组值 对应于传入的每个坐标. order参数仅确定插值的顺序.
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阶插值一个平面,因此它应该提取特定平面上的精确值.如果我错了,请纠正我.
I am not sure of how this actual works, but from what I understand this should interpolate a single plane with 0 order, so it should extract the exact value on a specific plane. Please correct me if I am wrong.
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")
奇怪的是,它不起作用.偶数索引还可以,但奇数索引是连续平面的副本.
Strangely it doesn't work. The even indexes are ok but the odd ones are a copy of the contiguous planes.
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.]
我认为某处肯定有一个愚蠢的错误,但我不知道.有什么主意吗?
I think there must be a stupid mistake somewhere, but I have no clue. Any idea?
推荐答案
像元中心位于z = 0.5、1.5、2.5等位置,因此FiPy最好找到最接近z = 0的像元, 1,2,...
The cell centers are located at z=0.5, 1.5, 2.5, ... so FiPy is doing it's best to find the nearest cells to z=0, 1, 2, ...
尝试
var2D.setValue(var3D((x2D, y2D,
zcut * ones(var2D.mesh.numberOfCells) + dx/2.), order=0))
这篇关于如何从FiPy(3D到2D)中的3D变量中提取平面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!