Advertisement
Guest User

Untitled

a guest
Dec 18th, 2016
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.56 KB | None | 0 0
  1. import numpy as np
  2. from vtk.numpy_interface import algorithms as algs
  3. from vtk.numpy_interface import dataset_adapter as dsa
  4.  
  5.  
  6. # "Boilerplate" code to get distribution information
  7. executive = self.GetExecutive()
  8. outInfo = executive.GetOutputInformation(0)
  9. pid = outInfo.Get(executive.UPDATE_PIECE_NUMBER())
  10.  
  11. exts = [executive.UPDATE_EXTENT().Get(outInfo, i) for i in xrange(6)]
  12. exts_slice_x = slice(exts[0], exts[1]+1)
  13. exts_slice_y = slice(exts[2], exts[3]+1)
  14. exts_slice_z = slice(exts[4], exts[5]+1)
  15. exts_slice = (exts_slice_x, exts_slice_y, exts_slice_z)
  16. whole = [executive.WHOLE_EXTENT().Get(outInfo, i) for i in xrange(6)]
  17.  
  18. dims = [exts[1]-exts[0]+1, exts[3]-exts[2]+1, exts[5]-exts[4]+1]
  19. global_dims = [whole[1]-whole[0]+1, whole[3]-whole[2]+1, whole[5]-whole[4]+1]
  20.  
  21. output.SetExtent(exts)
  22.  
  23. # Read the binary cartesian file function
  24. def readcart(fname, dims):
  25.     dims = np.array(dims)  # Easier to use np array to get count of items
  26.     with open(fname, 'rb') as f:
  27.         # Fortran records have INTEGER*4 headers which are useless here
  28.         f.seek(4)
  29.  
  30.         data = np.fromfile(f, dtype=np.float32, count=np.prod(dims), sep="")
  31.         # Order="F" is the key here
  32.         return data.reshape(dims, order="F")
  33.  
  34. # Construct grid
  35. xaxis = np.linspace(-1, 1, global_dims[0])[exts_slice_x]
  36. yaxis = np.linspace(-1, 1, global_dims[1])[exts_slice_y]
  37. zaxis = np.linspace(-1, 1, global_dims[2])[exts_slice_z]
  38.  
  39. output.SetXCoordinates(dsa.numpyTovtkDataArray(xaxis , "X"))
  40. output.SetYCoordinates(dsa.numpyTovtkDataArray(yaxis , "Y"))
  41. output.SetZCoordinates(dsa.numpyTovtkDataArray(zaxis , "Z"))
  42.  
  43. # Get data
  44. if directory[-1] != r"/":
  45.     directory = directory + "/"
  46. xname = directory + "field_bx.{}.out".format(filenum)
  47. yname = directory + "field_by.{}.out".format(filenum)
  48. zname = directory + "field_bz.{}.out".format(filenum)
  49.  
  50.  
  51. # Reshaping is because I need two axes to hstack and get (3n, 3). If I ravel
  52. # I get (3n,) with hstack
  53. # Must use "F" order again as reshaping only changes access order, not
  54. # memory location (implying that every reshaping must use "F" order)
  55. bx = readcart(xname, global_dims)[exts_slice].reshape(-1, 1, order="F")
  56. by = readcart(yname, global_dims)[exts_slice].reshape(-1, 1, order="F")
  57. bz = readcart(zname, global_dims)[exts_slice].reshape(-1, 1, order="F")
  58.  
  59. b = np.hstack((bx, by, bz))
  60.  
  61. # Convert to VTK and give names
  62. array = dsa.numpyTovtkDataArray(b, "B")
  63. array.SetComponentName(0, "B_x")
  64. array.SetComponentName(1, "B_y")
  65. array.SetComponentName(2, "B_z")
  66.  
  67. # Associate data to grid
  68. output.GetPointData().AddArray(array)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement