Skip to content

Commit 4b5a87d

Browse files
germa89akaszynski
andauthored
Feat/Adding extended examples library. (#613)
* Added index. Added ex 01. Resolves #612 Resolves #567 * Fixing grammar * Fixing grammar * Fixing format * Format fixing. * Format fixing * Renaming files to keep conventions. * Space format fixing * Format fixing * Small changes * Added more info to the examples. * Apply suggestions from code review Approved review changes made by @alex Co-authored-by: Alex Kaszynski <[email protected]> * Small format fix. Co-authored-by: Alex Kaszynski <[email protected]>
1 parent e4f8db5 commit 4b5a87d

File tree

7 files changed

+984
-0
lines changed

7 files changed

+984
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
.. _extended_example01:
2+
3+
4+
Example 01: Gmsh Example
5+
========================
6+
7+
Objective
8+
---------
9+
Demonstrate the interoperability of PyAnsys with ``gmsh``, a very well known
10+
open source Python meshing library.
11+
12+
For more information about ``gmsh`` please visit its website: `Gmsh <https://gmsh.info/>`_.
13+
14+
Description
15+
-----------
16+
In this example the interoperability of PyAnsys with the open source mesher ``gmsh`` is demonstrated.
17+
Using ``gmsh`` we import an external geometry file in STL format and then the
18+
geometry is imported into PyMAPDL using the
19+
`pymapdl-reader <https://github.com/pyansys/pymapdl-reader>`_ library.
20+
21+
This example is composed of several files.
22+
23+
* ``gmsh_converter.py``: Load a STEP file, mesh it, and save it as a gmsh file.
24+
* ``mesh_converter``: Convert the ``*.msh`` file into an Ansys CDB database format file (archive file).
25+
* ``modal_analysis.py``: Import CDB database, setup the modal analysis and run
26+
it. It also shows an animation of the first mode and save it
27+
to a gif file named ``animation.gif``.
28+
29+
30+
Requirements
31+
------------
32+
You need to have ``gmsh`` installed. You can install it using ``pip``:
33+
34+
.. code-block::
35+
36+
pip install gmsh
37+
38+
39+
Source code
40+
-----------
41+
42+
``gmsh_generator.py``
43+
~~~~~~~~~~~~~~~~~~~~~
44+
45+
.. literalinclude:: gmsh_generator.py
46+
:linenos:
47+
:language: python
48+
49+
50+
``mesh_converter.py``
51+
~~~~~~~~~~~~~~~~~~~~~
52+
53+
.. literalinclude:: mesh_converter.py
54+
:linenos:
55+
:language: python
56+
57+
58+
``modal_analysis.py``
59+
~~~~~~~~~~~~~~~~~~~~~
60+
61+
.. literalinclude:: modal_analysis.py
62+
:linenos:
63+
:language: python
64+
65+
66+
67+
Notes
68+
-----
69+
70+
It is recommended that you copy all the files in a separate directory to make it
71+
easier to run the example.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""Using ``gmsh`` read the STEP file, mesh it, and save it as a ``*.msh`` file."""
2+
import gmsh
3+
4+
gmsh.initialize()
5+
gmsh.option.setNumber("General.Terminal", 1)
6+
7+
gmsh.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):
11+
filename = 'pf_coil_case_1.stp'
12+
v = gmsh.model.occ.importShapes(filename)
13+
14+
15+
# Get the bounding box of the volume:
16+
gmsh.model.occ.synchronize()
17+
18+
# Specify a global mesh size and mesh the partitioned model:
19+
gmsh.option.setNumber("Mesh.CharacteristicLengthMin", 10)
20+
gmsh.option.setNumber("Mesh.CharacteristicLengthMax", 10)
21+
gmsh.model.mesh.generate(3)
22+
gmsh.write("from_gmsh.msh")
23+
24+
gmsh.finalize()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
This script convert the file in `gmsh` format to ANSYS `CDB` format.
3+
"""
4+
5+
import pyvista as pv
6+
from ansys.mapdl.reader import save_as_archive
7+
8+
filename = 'from_gmsh.msh'
9+
mesh = pv.read_meshio(filename)
10+
# mesh.plot() # optionally plot the mesh
11+
mesh.points /= 1000
12+
save_as_archive('archive.cdb', mesh)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""Import the CDB database, setup modal analysis and run it."""
2+
3+
4+
from ansys.mapdl.core import launch_mapdl
5+
6+
mapdl = launch_mapdl(override=True, additional_switches="-smp")
7+
filename = "archive.cdb"
8+
mapdl.cdread("db", filename)
9+
mapdl.save()
10+
11+
# verify cells are valid
12+
mapdl.prep7()
13+
mapdl.shpp("SUMM")
14+
15+
# specify material properties
16+
# using aprox values for AISI 5000 Series Steel
17+
mapdl.units("SI")
18+
mapdl.mp("EX", 1, 200e9) # Elastic moduli in Pa (kg/(m*s**2))
19+
mapdl.mp("DENS", 1, 7700) # Density in kg/m3
20+
mapdl.mp("NUXY", 1, 0.3) # Poissons Ratio
21+
mapdl.et(1, 181) # ! ET,1,SHELL181 ! SHELL181
22+
mapdl.keyopt(
23+
1, 3, 2
24+
) # ! Option for the shell. Integration option: 'Full integration with incompatible modes'
25+
mapdl.sectype(1, "SHELL")
26+
mapdl.secdata(1, 1, 0, 3) # ! which means: SECDATA,TK, MAT, THETA, NUMPT, LayerName
27+
mapdl.emodif("ALL", "MAT", 1) # ! Setting material id
28+
mapdl.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.
34+
mapdl.esel("S", "type", "", 4) # selecting elements with type 4 = shell181
35+
mapdl.edele("all")
36+
mapdl.allsel()
37+
38+
# Run an unconstrained modal analysis
39+
mapdl.run("/SOLU")
40+
mapdl.outres("all")
41+
mapdl.antype("MODAL") # default NEW
42+
mapdl.modopt("LANB", 20, 1)
43+
mapdl.solve(verbose=False)
44+
mapdl.save("solved")
45+
46+
mapdl.post1()
47+
mapdl.set("FIRST")
48+
49+
result = mapdl.result
50+
51+
result.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+
63+
mapdl.exit()

0 commit comments

Comments
 (0)