Gmsh example#

Objective#

This example demonstrate the interoperability of PyAnsys with Gmsh, a very well known open source Python meshing library. For more information, visit the Gmsh website: Gmsh.

Description#

Gmsh is used to import an external geometry file in STL format. The pymapdl-reader library is then used to import the geometry into PyMAPDL.

This example makes use of these files:

  • gmsh_converter.py: Loads a STEP file, meshes it, and saves it as a Gmsh file.

  • mesh_converter: Converts the MSH file into an Ansys CDB database format file (archive file).

  • modal_analysis.py: Imports the CDB database, sets up the modal analysis, and runs it. It also shows an animation of the first mode and saves it to a GIF file named animation.gif.

Requirements#

You must have Gmsh installed. You can install it using pip:

pip install gmsh

Source code#

gmsh_generator.py file#

 1"""Using ``gmsh``, read the STEP file, mesh it, and save it as a MSH file."""
 2import gmsh
 3
 4gmsh.initialize()
 5gmsh.option.setNumber("General.Terminal", 1)
 6
 7gmsh.model.add("t20")
 8
 9# Load a STEP file (using `importShapes' instead of `merge' allows to directly
10# retrieve the tags of the highest dimensional imported entities):
11filename = "pf_coil_case_1.stp"
12v = gmsh.model.occ.importShapes(filename)
13
14
15# Get the bounding box of the volume:
16gmsh.model.occ.synchronize()
17
18# Specify a global mesh size and mesh the partitioned model:
19gmsh.option.setNumber("Mesh.CharacteristicLengthMin", 10)
20gmsh.option.setNumber("Mesh.CharacteristicLengthMax", 10)
21gmsh.model.mesh.generate(3)
22gmsh.write("from_gmsh.msh")
23
24gmsh.finalize()

mesh_converter.py file#

 1"""
 2This script convert the file in `gmsh` format to ANSYS `CDB` format.
 3"""
 4
 5from ansys.mapdl.reader import save_as_archive
 6import pyvista as pv
 7
 8filename = "from_gmsh.msh"
 9mesh = pv.read_meshio(filename)
10# mesh.plot()  # optionally plot the mesh
11mesh.points /= 1000
12save_as_archive("archive.cdb", mesh)

Notes#

You should copy all the files in a separate directory to make running the example easier.