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)
modal_analysis.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"""Import the CDB database, setup modal analysis and run it."""
24
25
26from ansys.mapdl.core import launch_mapdl
27
28mapdl = launch_mapdl(override=True, additional_switches="-smp")
29filename = "archive.cdb"
30mapdl.cdread("db", filename)
31mapdl.save()
32
33# verify cells are valid
34mapdl.prep7()
35mapdl.shpp("SUMM")
36
37# specify material properties
38# using aprox values for AISI 5000 Series Steel
39mapdl.units("SI")
40mapdl.mp("EX", 1, 200e9) # Elastic moduli in Pa (kg/(m*s**2))
41mapdl.mp("DENS", 1, 7700) # Density in kg/m3
42mapdl.mp("NUXY", 1, 0.3) # Poissons Ratio
43mapdl.et(1, 181) # ! ET,1,SHELL181 ! SHELL181
44mapdl.keyopt(
45 1, 3, 2
46) # ! Option for the shell. Integration option: 'Full integration with incompatible modes'
47mapdl.sectype(1, "SHELL")
48mapdl.secdata(1, 1, 0, 3) # ! which means: SECDATA,TK, MAT, THETA, NUMPT, LayerName
49mapdl.emodif("ALL", "MAT", 1) # ! Setting material id
50mapdl.emodif("ALL", "REAL", 1) # ! Setting real constant
51
52# By setting the section type (`SECTYPE`) the model will run and solve.
53
54# The model has solid and shell elements.
55# We don't need both, hence I'm going to delete the shell elements.
56mapdl.esel("S", "type", "", 4) # selecting elements with type 4 = shell181
57mapdl.edele("all")
58mapdl.allsel()
59
60# Run an unconstrained modal analysis
61mapdl.run("/SOLU")
62mapdl.outres("all")
63mapdl.antype("MODAL") # default NEW
64mapdl.modopt("LANB", 20, 1)
65mapdl.solve(verbose=False)
66mapdl.save("solved")
67
68mapdl.post1()
69mapdl.set("FIRST")
70
71result = mapdl.result
72
73result.animate_nodal_displacement(
74 4,
75 show_edges=False,
76 lighting=True,
77 loop=True,
78 add_text=False,
79 nangles=30,
80 # displacement_factor=50,
81 n_frames=100,
82 movie_filename="animation.gif",
83)
84
85mapdl.exit()
Notes#
您应将所有文件复制到一个单独的目录中,以便于运行示例。