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# Copyright (C) 2016 - 2024 ANSYS, Inc. and/or its affiliates.
 2# SPDX-License-Identifier: MIT
 3#
 4#
 5# Permission is hereby granted, free of charge, to any person obtaining a copy
 6# of this software and associated documentation files (the "Software"), to deal
 7# in the Software without restriction, including without limitation the rights
 8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 9# copies of the Software, and to permit persons to whom the Software is
10# furnished to do so, subject to the following conditions:
11#
12# The above copyright notice and this permission notice shall be included in all
13# copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21# SOFTWARE.
22
23"""Using ``gmsh``, read the STEP file, mesh it, and save it as a MSH file."""
24import gmsh
25
26gmsh.initialize()
27gmsh.option.setNumber("General.Terminal", 1)
28
29gmsh.model.add("t20")
30
31# Load a STEP file (using `importShapes' instead of `merge' allows to directly
32# retrieve the tags of the highest dimensional imported entities):
33filename = "pf_coil_case_1.stp"
34v = gmsh.model.occ.importShapes(filename)
35
36
37# Get the bounding box of the volume:
38gmsh.model.occ.synchronize()
39
40# Specify a global mesh size and mesh the partitioned model:
41gmsh.option.setNumber("Mesh.CharacteristicLengthMin", 10)
42gmsh.option.setNumber("Mesh.CharacteristicLengthMax", 10)
43gmsh.model.mesh.generate(3)
44gmsh.write("from_gmsh.msh")
45
46gmsh.finalize()

mesh_converter.py file#

 1# Copyright (C) 2016 - 2024 ANSYS, Inc. and/or its affiliates.
 2# SPDX-License-Identifier: MIT
 3#
 4#
 5# Permission is hereby granted, free of charge, to any person obtaining a copy
 6# of this software and associated documentation files (the "Software"), to deal
 7# in the Software without restriction, including without limitation the rights
 8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 9# copies of the Software, and to permit persons to whom the Software is
10# furnished to do so, subject to the following conditions:
11#
12# The above copyright notice and this permission notice shall be included in all
13# copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21# SOFTWARE.
22
23"""
24This script convert the file in `gmsh` format to ANSYS `CDB` format.
25"""
26
27from ansys.mapdl.reader import save_as_archive
28import pyvista as pv
29
30filename = "from_gmsh.msh"
31mesh = pv.read_meshio(filename)
32# mesh.plot()  # optionally plot the mesh
33mesh.points /= 1000
34save_as_archive("archive.cdb", mesh)

Notes#

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