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 namedanimation.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)
modal_analysis.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"""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#
You should copy all the files in a separate directory to make running the example easier.