Advertisement
Guest User

RVE.py

a guest
Jan 17th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.80 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from math import *
  4.  
  5. Side_size = 64
  6. Fiber_number = 5
  7. VTKFileName = "Test.vtk"
  8. Option = "3D" #"2D"
  9.  
  10. # RVE generation, 0 is for the matrix
  11. RVE = np.zeros([Side_size ,Side_size ])
  12.  
  13. #Add a fiber at coordinate X,Y with radius R
  14. R = 10
  15.  
  16. def placeFiber(X,Y,R,RVE):
  17.     # 1 is the fiber material
  18.     for i in range(len(RVE)):
  19.         for j in range(len(RVE[0])):
  20.             if  sqrt((i-X)**2+(j-Y)**2) < R:
  21.                 RVE[i][j] = 1
  22.     return RVE
  23.  
  24. FiberCoords = np.random.random((Fiber_number,2))*Side_size
  25.  
  26. for fiber in FiberCoords:
  27.     RVE = placeFiber(fiber[0],fiber[1],R,RVE)
  28.  
  29. def VTKize(RVE,FileName,Option):
  30.     # Header
  31.     OutputStr = ""
  32.     OutputStr += "# vtk DataFile Version 2.0 \n"
  33.     OutputStr += FileName + "\n"
  34.     OutputStr += "ASCII\nDATASET STRUCTURED_POINTS\n"
  35.     OutputStr += "DIMENSIONS "+str(Side_size)+" "+str(Side_size)+" "+str(Side_size)+"\n"
  36.     OutputStr += "ORIGIN 0 0 0\nSPACING 1 1 1\n"
  37.     OutputStr += "CELL_DATA "+str(Side_size*Side_size*Side_size)+"\n"
  38.  
  39.     # Proper data
  40.     # That is stored in the RVE matrix in this program
  41.     if Option == "3D":
  42.         for channel in range(len(RVE)):
  43.             for row in range(len(RVE)):
  44.                 for line in range(len(RVE[row])):
  45.                     OutputStr += str(int(RVE[row,line]))+" "
  46.                 OutputStr += "\n"
  47.     elif Option == "2D":
  48.         for row in range(len(RVE)):
  49.             for line in range(len(RVE[row])):
  50.                 OutputStr += str(int(RVE[row,line]))+" "
  51.             OutputStr += "\n"
  52.     # Return the string
  53.  
  54.     return OutputStr
  55.  
  56.  
  57. VTKstring = VTKize(RVE,VTKFileName,Option)
  58.  
  59. # Write the data in a File
  60. VTKFile = open(VTKFileName,"w")
  61. VTKFile.write(VTKstring)
  62. VTKFile.close()
  63.  
  64.  
  65. plt.imshow(RVE)
  66. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement