0

How to read and visualize MESH file using PyVista

I have created a Cartesian grid model using Mesh Maker which is given the attached MESH file. How to read MESH file and visualize the grid model using PyVista? 

11 replies

null
    • Keurfon_Luu
    • 1 yr ago
    • Reported - view

    Hi Refaat G Hashish ,

    Generally, given any MESH file, you can use the function `toughio.meshmaker.triangulate`. As you guess from the name, this function performs a Delaunay triangulation to reconstruct a mesh from a cloud of points (here, the X, Y, Z coordinates of the elements in the MESH file), so it not the actual mesh generated by MESHMAKER.

    import toughio
    
    parameters = toughio.read_input("MESH")
    points = [v["center"] for v in parameters["elements"].values()]
    mesh = toughio.meshmaker.triangulate(points)
    mesh.plot(show_edges=True)
    

    Otherwise, you can also use the function `toughio.meshmaker.from_meshmaker` to generate a mesh from your TOUGH input file that you used to generate your mesh with MESHMAKER (a file that contains a MESHMAKER block).

    mesh = toughio.meshmaker.from_meshmaker("INFILE")
    mesh.plot(show_edges=True)
    

    Alternatively, why not simply use the function `toughio.meshmaker.structured_grid` or `toughio.meshmaker.cylindric_grid` to generate your mesh (and MESH file via the method `mesh.write_tough`)?

    import numpy as np
    
    dx = np.arange(5)
    dy = np.arange(6)
    dz = np.arange(7)
    mesh = toughio.meshmaker.structured_grid(dx, dy, dz)
    mesh.write_tough()
    mesh.plot(show_edges=True)
    
      • Refaat_G_Hashish
      • 1 yr ago
      • Reported - view

      Keurfon Luu Thank you so much. All of the methods are working fine. I wonder why only a single material is shown on the grid model while the MESH file includes grid cells with different materials. Is there a way to show all the materials on the grid model?

      • Keurfon_Luu
      • 1 yr ago
      • Reported - view

      Refaat G Hashish I am not sure how you got material defined in your MESH file since TOUGH built-in MESHMAKER does not have such feature.

      Anyway, none of the previous sample codes have imported the material properties from your MESH file.

      import toughio
      
      parameters = toughio.read_input("MESH")
      points = [v["center"] for v in parameters["elements"].values()]
      materials = [v["material"] for v in parameters["elements"].values()]
      mesh = toughio.meshmaker.triangulate(points)
      mesh.add_point_data("material", materials)
      mesh.plot(scalars="material", show_edges=True)
      

      If you use triangulation, you should use the method `add_point_data`, otherwise you should use `set_material`.

      • Refaat_G_Hashish
      • 1 yr ago
      • Reported - view

      Keurfon Luu Thank you. It now shows the material types on the grid model. By the way, I have assigned the material types to the MESH file after creating it using MESHMAKER using the following python code:

      ------------------------------------------------------------------------------------------------------

      import toughio

      # Read file with block ELEME (e.g. MESH)
      parameters = toughio.read_input("MESH")

      # Loop over elements
      for label, element in parameters["elements"].items():
          x, y, z = element["center"]

          # Assign material name depending on element's depth
          if 0.0 >= z > -10.0:
              element["material"] = "mat01"
          elif -10.0 >= z > -20.0:
              element["material"] = "mat02"
          elif -20.0 >= z > -30.0:
              element["material"] = "mat03"
          # and so on

      # Write your file back with modified materials
      toughio.write_input("MESH", parameters)

      --------------------------------------------------------------------------------------------------------

      • Keurfon_Luu
      • 1 yr ago
      • Reported - view

      Refaat G Hashish Since you are using toughio to set the material, why not directly use toughio built-in mesh generation functions to generate your structured mesh?

      dx = 10 * [10.0]
      mesh = toughio.meshmaker.structured_grid(dx, dx, dx, material="MID")
      mesh.set_material("BOT", mesh.filter.box(z0=-100, dz=20))
      mesh.set_material("TOP", mesh.filter.box(z0=-20, dz=20))
      mesh.plot(show_edges=True)
      

      I guess that's on me and the missing documentation about all these features.

      • Refaat_G_Hashish
      • 1 yr ago
      • Reported - view

      Keurfon Luu Thank you. I have a final question. Is there a way to modify the background color?

      • Keurfon_Luu
      • 1 yr ago
      • Reported - view

      Refaat G Hashish The method `plot` relies on PyVista, so you can refer to PyVista's documentation to customize your plots. For instance, to change the color of your background, see here: https://docs.pyvista.org/api/plotting/_autosummary/pyvista.themes.ParaViewTheme.background.html?highlight=background+color

      • Refaat_G_Hashish
      • 1 yr ago
      • Reported - view

      Keurfon Luu I got the following error message. 

      Traceback (most recent call last):

        File "<ipython-input-11-4d803777092a>", line 1, in <module>
          pyvista.global_theme.background = 'white'

      AttributeError: module 'pyvista' has no attribute 'global_theme'

      • Refaat_G_Hashish
      • 1 yr ago
      • Reported - view

      Keurfon Luu Got it. Thank you.

      • Refaat_G_Hashish
      • 1 yr ago
      • Reported - view

      Keurfon Luu Is it possible to use the function "toughio.meshmaker" to reconstruct a mesh from a cloud of points such that grid cells are rectangular?

      • Keurfon_Luu
      • 1 yr ago
      • Reported - view

      Refaat G Hashish Generally speaking, no. To reconstruct an actual mesh for visualization, you need the corner points coordinates of the cells which are lacking in TOUGH generated MESH files (X, Y, Z are the coordinates of cell centers).

      However, in case your mesh is actually a structured grid, you can try to voxelize it using `toughio.meshmaker.voxelize` (https://keurfonluu.github.io/toughio/api/mesh.html?highlight=voxelize#toughio.meshmaker.voxelize) which will try to reconstruct the original mesh if you provide the coordinate of the lower left corner point. The only issue is that the X, Y, Z coordinates in MESH files generated by TOUGH's built-in MESHMAKER usually are not accurate enough (only 3 decimal places) and the algorithm will fail because some elements have the same coordinates.

Content aside

  • 1 yr agoLast active
  • 11Replies
  • 351Views
  • 2 Following