问题
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.
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
Output:
[ 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.]
The 3D variable looks correctly filled.
First, I tried to use the way described at this link 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.
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?
回答1:
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, ...
Try
var2D.setValue(var3D((x2D, y2D,
zcut * ones(var2D.mesh.numberOfCells) + dx/2.), order=0))
来源:https://stackoverflow.com/questions/29863958/how-to-extract-a-plane-from-a-3d-variable-in-fipy-3d-to-2d