/EXAMPLES/Mount_StHelens/mesh_mount_stl.py

https://gitlab.com/Aaeinstein54/specfem3d · Python · 198 lines · 89 code · 33 blank · 76 comment · 6 complexity · 5536eebf3093e65952fc52ab78191715 MD5 · raw file

  1. #!/usr/bin/env python
  2. #############################################################
  3. #
  4. # script uses STL surface formats
  5. #
  6. # note: this script seems only to work with CUBIT version 12.2
  7. #
  8. # ( try out script mesh_mount.py when using a different CUBIT version)
  9. #
  10. #############################################################
  11. import cubit
  12. import boundary_definition
  13. import cubit2specfem3d
  14. import os
  15. import sys
  16. import os.path
  17. # time stamp
  18. print "#" + time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
  19. # working directory
  20. cwd = os.getcwd()
  21. print "#current working directory: " + str(cwd)
  22. if cwd[len(cwd)-14:len(cwd)] != "Mount_StHelens":
  23. print ""
  24. print "#please run this script from example directory: SPECFEM3D/example/Mount_StHelens/"
  25. print ""
  26. cubit.cmd('version')
  27. cubit.cmd('reset')
  28. os.system('rm -f topo_brick.stl topo_vol.stl topo_vol2.stl')
  29. print "running meshing script..."
  30. print ""
  31. print "note: this script uses topography surface in STL format"
  32. print " meshing will take around 2 min"
  33. print ""
  34. # note: this is a workaround to use STL file formats rather than ACIS formats.
  35. # for our purpose to create a simple mesh, this STL formats are faster
  36. # uses developer commands
  37. cubit.cmd('set developer commands on')
  38. cubit.cmd('set import mesh tolerance 100')
  39. #############################################################
  40. #
  41. # 1. step: creates temporary brick volume in STL format
  42. #
  43. #############################################################
  44. # creates temporary brick volume and exports in STL format
  45. # new brick volume (depth will become 1/2 * 20,000 = 10,000 m)
  46. cubit.cmd('brick x 15000 y 22000 z 20000')
  47. # moves volume to UTM coordinates of topography surface
  48. cubit.cmd('volume 1 move x 561738. y 5116370. z 0 ')
  49. # saves as STL body
  50. cubit.cmd('export stl ascii "topo_brick.stl" overwrite')
  51. # clear
  52. cubit.cmd('reset')
  53. #checks if new file available
  54. if not os.path.exists("topo_brick.stl"):
  55. print ""
  56. print "error creating new STL file topo_brick.stl, please check manually..."
  57. print ""
  58. cubit.cmd('pause')
  59. #############################################################
  60. #
  61. # 2. step: imports topography surface and brick volume in STL format
  62. #
  63. #############################################################
  64. # topography surface
  65. if os.path.exists("topo.stl"):
  66. print "opening existing topography surface"
  67. # previously run, just reopen the cubit file
  68. cubit.cmd('import stl "topo.stl" merge stitch')
  69. else:
  70. # topo surface doesn't exist yet, this creates it:
  71. print "reading in topography surface"
  72. # reads in topography points and creates sheet surface
  73. execfile("./read_topo.py")
  74. # clear
  75. cubit.cmd('reset')
  76. # now reopen the cubit file
  77. cubit.cmd('import stl "topo.stl" merge stitch')
  78. # re-opens brick in STL format
  79. cubit.cmd('import stl "topo_brick.stl" merge stitch')
  80. # imprints topography surfaces into brick volume, creates connected surfaces
  81. # note: this is a develop feature
  82. cubit.cmd('imprint all')
  83. cubit.cmd('version')
  84. # cubit 12.2 specific
  85. # exports as STL only surfaces which will create new volume
  86. cubit.cmd('export stl ascii "topo_vol.stl" surface 9 5 6 8 11 13 overwrite')
  87. # clear
  88. cubit.cmd('reset')
  89. #checks if new file available
  90. if not os.path.exists("topo_vol.stl"):
  91. print ""
  92. print "error creating new STL file topo_vol.stl, please check manually..."
  93. print ""
  94. cubit.cmd('pause')
  95. #############################################################
  96. #
  97. # 3. step: manipulate STL file to create a single volume
  98. #
  99. #############################################################
  100. os.system('awk \'BEGIN{print \"solid Body_1\";}{if($0 !~ /solid/) print $0;}END{print \"endsolid Body_1\";}\' topo_vol.stl > topo_vol2.stl')
  101. #checks if new file available
  102. if not os.path.exists("topo_vol2.stl"):
  103. print ""
  104. print "error creating new STL file topo_vol2.stl, please check manually..."
  105. print ""
  106. cubit.cmd('pause')
  107. #############################################################
  108. #
  109. # 4. step: import STL volume and create mesh
  110. #
  111. #############################################################
  112. # re-opens new volume in STL format
  113. cubit.cmd('import stl "topo_vol2.stl" merge stitch')
  114. # Meshing the volumes
  115. elementsize = 2000.0
  116. cubit.cmd('volume all size '+str(elementsize))
  117. # sets meshing type
  118. # uses a sweep algorithm in vertical (Z) direction
  119. cubit.cmd('volume all scheme sweep Vector 0 0 1')
  120. # initial coarse mesh
  121. cubit.cmd('mesh volume all')
  122. # draw/update mesh lines for visualization
  123. # this will draw also the tripling layer mesh lines
  124. cubit.cmd('draw volume all')
  125. # optional smoothing to improve mesh quality (takes up to 5 min)
  126. cubit.cmd('volume all smooth scheme condition number beta 1.0 cpu 5')
  127. cubit.cmd('smooth volume all')
  128. # refines global mesh
  129. cubit.cmd('refine volume 1 numsplit 1')
  130. cubit.cmd('draw volume all')
  131. # optional smoothing
  132. cubit.cmd('smooth volume all')
  133. # refines elements at topography surface
  134. cubit.cmd('refine surface 2 numsplit 1 bias 1.0 depth 1')
  135. cubit.cmd('draw volume all')
  136. # optional smoothing to improve mesh quality (takes up all 10 min if beta is 2.0)
  137. cubit.cmd('volume all smooth scheme condition number beta 2.3 cpu 10')
  138. cubit.cmd('smooth volume all')
  139. cubit.cmd('draw volume all')
  140. #### End of meshing
  141. ###### This is boundary_definition.py of GEOCUBIT
  142. #..... which extracts the bounding faces and defines them into blocks
  143. boundary_definition.entities=['face']
  144. boundary_definition.define_bc(boundary_definition.entities,parallel=True)
  145. #### Define material properties for the 3 volumes ################
  146. cubit.cmd('#### DEFINE MATERIAL PROPERTIES #######################')
  147. cubit.cmd('block 1 name "elastic 1" ') # elastic material region
  148. cubit.cmd('block 1 attribute count 6')
  149. cubit.cmd('block 1 attribute index 1 1') # flag for material: 1 for 1. material
  150. cubit.cmd('block 1 attribute index 2 2800') # vp
  151. cubit.cmd('block 1 attribute index 3 1500') # vs
  152. cubit.cmd('block 1 attribute index 4 2300') # rho
  153. cubit.cmd('block 1 attribute index 5 150.0') # Qmu
  154. cubit.cmd('block 1 attribute index 6 0 ') # anisotropy_flag
  155. # optional saves backups
  156. cubit.cmd('export mesh "top.e" dimension 3 overwrite')
  157. cubit.cmd('save as "meshing.cub" overwrite')
  158. # cleanup
  159. os.system('rm -f topo_brick.stl topo_vol.stl topo_vol2.stl')
  160. #### Export to SPECFEM3D format using cubit2specfem3d.py of GEOCUBIT
  161. os.system('mkdir -p MESH')
  162. cubit2specfem3d.export2SPECFEM3D('MESH')
  163. # all files needed by SCOTCH are now in directory MESH
  164. # time stamp
  165. print "#" + time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())