Advertisement
Guest User

ParaView Voidfraction Filter by richti83

a guest
Feb 9th, 2022
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.56 KB | None | 0 0
  1. ntry=5000 #increase for better accuracy, decrease for faster performance
  2.  
  3. from random import uniform
  4. input1 = self.GetInputDataObject(0, 0)
  5. output = self.GetOutputDataObject(0)
  6. b=input1.GetBounds()
  7. xlow,xhi,ylow,yhi,zlow,zhi=b[:6]
  8. P=input1.GetPointData()
  9. COUNT=input1.GetNumberOfPoints()
  10. #step 1: estimate bounding volume
  11. for i in range(0,COUNT):
  12.     coord = input1.GetPoint(i)
  13.     x, y, z = coord[:3]
  14.     r=P.GetArray("radius").GetValue(i)
  15.     if x<=xlow:
  16.         xlow=x-r
  17.     if y<=ylow:
  18.         ylow=y-r
  19.     if z<=zlow:
  20.         zlow=z-r
  21.     if x>=xhi:
  22.         xhi=x+r
  23.     if y>=yhi:
  24.         yhi=y+r
  25.     if z>=zhi:
  26.         zhi=z+r
  27. Vb=(xhi-xlow)*(yhi-ylow)*(zhi-zlow)
  28.  
  29. #Step 2: Monte Carlo Integration of the volume of the spheres
  30. hit=0.0
  31.  
  32. for t in range(0,ntry):
  33.     xtry=uniform(xlow,xhi)
  34.     ytry=uniform(ylow,yhi)
  35.     ztry=uniform(zlow,zhi)
  36.     known=0
  37.     for i in range(0,COUNT):    #for all centers of spheres check distance to X_try
  38.      coord = input1.GetPoint(i)
  39.      x, y, z = coord[:3]
  40.      r=P.GetArray("radius").GetValue(i)
  41.      d=0.0
  42.      d+=(xtry-x)*(xtry-x)
  43.      d+=(ytry-y)*(ytry-y)
  44.      d+=(ztry-z)*(ztry-z)
  45.      if (d<r*r):
  46.         #point is inside this sphere
  47.         hit+=1.0
  48.         known=1
  49.      if (known==1):
  50.         break
  51.  
  52. vol=hit/float(ntry)*Vb
  53. void=(Vb-vol)*100/Vb
  54.  
  55. outputarray = vtk.vtkStringArray()
  56. outputarray.SetName("Text")
  57. outputarray.SetNumberOfTuples(1)
  58. outputarray.SetValue(0,"Spheres=%d\nV_S=%1.6f m3\nV_B=%1.6f m3\nVoidfraction=%1.1f%%" % (COUNT,vol,Vb,void))
  59. output.GetRowData().AddArray(outputarray)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement