Gmsh example#

Objective#

该示例演示了 PyAnsys 与 Gmsh 的互操作性, Gmsh 是一个非常著名的开源 Python 网格库。更多信息,请访问 Gmsh 网站:Gmsh

Description#

Gmsh 用于导入 STL 格式的外部几何体文件。 然后使用 PyMAPDL Reader 库将几何图形导入 PyMAPDL。

本示例使用以下这些文件:

  • gmsh_converter.py: 加载 STEP 文件,绘制网格并保存为 Gmsh 文件。

  • mesh_converter: 将 MSH 文件转换为 Ansys CDB 数据库格式文件(归档文件)。

  • modal_analysis.py: 导入 CDB 数据库、设置模态分析并运行。它还会显示一阶模态的动画,并将其保存到名为 animation.gif 的 GIF 文件中。

Requirements#

您必须安装 Gmsh。可以使用 pip 安装:

pip install gmsh

Source code#

gmsh_generator.py file#

 1# Copyright (C) 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"""使用 ``gmsh`` 读取 STEP 文件,将其网格化并保存为 MSH 文件。"""
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) 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#

您应将所有文件复制到一个单独的目录中,以便于运行示例。