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 namedanimation.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
5from ansys.mapdl.reader import save_as_archive
6import pyvista as pv
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)
modal_analysis.py
¶
1"""Import the CDB database, setup modal analysis and run it."""
2
3
4from ansys.mapdl.core import launch_mapdl
5
6mapdl = launch_mapdl(override=True, additional_switches="-smp")
7filename = "archive.cdb"
8mapdl.cdread("db", filename)
9mapdl.save()
10
11# verify cells are valid
12mapdl.prep7()
13mapdl.shpp("SUMM")
14
15# specify material properties
16# using aprox values for AISI 5000 Series Steel
17mapdl.units("SI")
18mapdl.mp("EX", 1, 200e9) # Elastic moduli in Pa (kg/(m*s**2))
19mapdl.mp("DENS", 1, 7700) # Density in kg/m3
20mapdl.mp("NUXY", 1, 0.3) # Poissons Ratio
21mapdl.et(1, 181) # ! ET,1,SHELL181 ! SHELL181
22mapdl.keyopt(
23 1, 3, 2
24) # ! Option for the shell. Integration option: 'Full integration with incompatible modes'
25mapdl.sectype(1, "SHELL")
26mapdl.secdata(1, 1, 0, 3) # ! which means: SECDATA,TK, MAT, THETA, NUMPT, LayerName
27mapdl.emodif("ALL", "MAT", 1) # ! Setting material id
28mapdl.emodif("ALL", "REAL", 1) # ! Setting real constant
29
30# By setting the section type (`SECTYPE`) the model will run and solve.
31
32# The model has solid and shell elements.
33# We don't need both, hence I'm going to delete the shell elements.
34mapdl.esel("S", "type", "", 4) # selecting elements with type 4 = shell181
35mapdl.edele("all")
36mapdl.allsel()
37
38# Run an unconstrained modal analysis
39mapdl.run("/SOLU")
40mapdl.outres("all")
41mapdl.antype("MODAL") # default NEW
42mapdl.modopt("LANB", 20, 1)
43mapdl.solve(verbose=False)
44mapdl.save("solved")
45
46mapdl.post1()
47mapdl.set("FIRST")
48
49result = mapdl.result
50
51result.animate_nodal_displacement(
52 4,
53 show_edges=False,
54 lighting=True,
55 loop=True,
56 add_text=False,
57 nangles=30,
58 # displacement_factor=50,
59 n_frames=100,
60 movie_filename="animation.gif",
61)
62
63mapdl.exit()
Notes¶
It is recommended that you copy all the files in a separate directory to make it easier to run the example.