Example 01: Gmsh Example

Objective

Demonstrate the interoperability of PyAnsys with gmsh, a very well known open source Python meshing library.

For more information about gmsh please visit its website: Gmsh.

Description

In this example the interoperability of PyAnsys with the open source mesher gmsh is demonstrated. Using gmsh we import an external geometry file in STL format and then the geometry is imported into PyMAPDL using the pymapdl-reader library.

This example is composed of several files.

  • gmsh_converter.py: Load a STEP file, mesh it, and save it as a gmsh file.

  • mesh_converter: Convert the *.msh file into an Ansys CDB database format file (archive file).

  • modal_analysis.py: Import CDB database, setup the modal analysis and run it. It also shows an animation of the first mode and save it to a gif file named animation.gif.

Requirements

You need to have gmsh installed. You can install it using pip:

pip install gmsh

Source code

gmsh_generator.py

 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

 1"""
 2This script convert the file in `gmsh` format to ANSYS `CDB` format.
 3"""
 4
 5import pyvista as pv
 6from ansys.mapdl.reader import save_as_archive
 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

It is recommended that you copy all the files in a separate directory to make it easier to run the example.