Plotting and Mesh Access#

PyMAPDL can load basic IGES geometry for analysis.

This example demonstrates loading basic geometry into MAPDL for analysis and demonstrates how to use the built-in Python specific plotting functionality.

This example also demonstrates some of the more advanced features of PyMAPDL including direct mesh access through VTK.

# sphinx_gallery_thumbnail_number = 3

import numpy as np

from ansys.mapdl import core as pymapdl
from ansys.mapdl.core import examples

mapdl = pymapdl.launch_mapdl()

Load Geometry#

Here we download a simple example bracket IGES file and load it into MAPDL. Since igesin must be in the AUX15 process

# note that this method just returns a file path
bracket_file = examples.download_bracket()

# load the bracket and then print out the geometry
mapdl.aux15()
mapdl.igesin(bracket_file)
print(mapdl.geometry)

Out:

MAPDL Selected Geometry
Keypoints:  188
Lines:      185
Areas:      73
Volumes:    1

Plotting#

PyMAPDL uses VTK and pyvista as a plotting backend to enable remotable (with 2021R1 and newer) interactive plotting. The common plotting methods (kplot, lplot, aplot, eplot, etc.) all have compatible commands that use the ansys.mapdl.core.plotting.general_plotter() function. You can configure this method with a variety of keyword arguments. For example:

mapdl.lplot(
    show_line_numbering=False,
    background="k",
    line_width=3,
    color="w",
    show_axes=False,
    show_bounds=True,
    title="",
    cpos="xz",
)
geometry

You can also configure a theme to enable consistent plotting across multiple plots. These theme parameters override any unset keyword arguments. For example:

my_theme = pymapdl.MapdlTheme()
my_theme.background = "white"
my_theme.cmap = "jet"  # colormap
my_theme.axes.show = False
my_theme.show_scalar_bar = False

mapdl.aplot(theme=my_theme)
geometry

Accessesing Element and Nodes Pythonically#

PyMAPDL also supports element and nodal plotting using eplot and nplot. First, mesh the bracket using SOLID187 elements. These are well suited to this geometry and the static structural analyses.

# set the preprocessor, element type and size, and mesh the volume
mapdl.prep7()
mapdl.et(1, "SOLID187")
mapdl.esize(0.075)
mapdl.vmesh("all")

# print out the mesh characteristics
print(mapdl.mesh)

Out:

ANSYS Mesh
  Number of Nodes:              50686
  Number of Elements:           32198
  Number of Element Types:      1
  Number of Node Components:    0
  Number of Element Components: 0

You can access the underlying finite element mesh as a VTK grid through the mesh.grid attribute.

grid = mapdl.mesh.grid
grid
HeaderData Arrays
UnstructuredGridInformation
N Cells32198
N Points50686
X Bounds-1.181e+00, 1.181e+00
Y Bounds-2.362e-01, 0.000e+00
Z Bounds-2.394e+00, 2.509e+00
N Arrays10
NameFieldTypeN CompMinMax
ansys_node_numPointsint3211.000e+005.069e+04
vtkOriginalPointIdsPointsint6410.000e+005.068e+04
origidPointsint6410.000e+005.068e+04
VTKorigIDPointsint6410.000e+005.068e+04
ansys_elem_numCellsint3211.000e+003.220e+04
ansys_real_constantCellsint3211.000e+001.000e+00
ansys_material_typeCellsint3211.000e+001.000e+00
ansys_etypeCellsint3211.000e+001.000e+00
ansys_elem_type_numCellsint3211.870e+021.870e+02
vtkOriginalCellIdsCellsint6410.000e+003.220e+04


This UnstructuredGrid contains a powerful API, including the ability to access the nodes, elements, original node numbers, all with the ability to plot the mesh and add new attributes and data to the grid.

grid.points  # same as mapdl.mesh.nodes

Out:

pyvista_ndarray([[-2.03111884e-01, -5.87401575e-02,  4.44426114e-04],
                 [-2.03111884e-01,  0.00000000e+00,  4.44426114e-04],
                 [-2.03111884e-01, -2.93700787e-02,  4.44426114e-04],
                 ...,
                 [-3.75851169e-01, -1.85457803e-01, -2.53642976e-01],
                 [ 4.87955026e-02, -1.81666560e-01, -4.74114746e-01],
                 [-5.38861990e-01, -1.81771018e-01, -7.19734818e-01]])

cell representation in VTK format

Out:

array([   10,  4237, 15212, ..., 35361, 36457, 35354])

Obtain node numbers of the grid

grid.point_data["ansys_node_num"]

Out:

pyvista_ndarray([    1,     2,     3, ..., 50684, 50685, 50686],
                dtype=int32)

Save arbitrary data to the grid

# must be sized to the number of points
grid.point_data["my_data"] = np.arange(grid.n_points)
grid.point_data

Out:

pyvista DataSetAttributes
Association     : POINT
Active Scalars  : my_data
Active Vectors  : None
Active Texture  : None
Active Normals  : None
Contains arrays :
    ansys_node_num          int32    (50686,)
    vtkOriginalPointIds     int64    (50686,)
    origid                  int64    (50686,)
    VTKorigID               int64    (50686,)
    my_data                 int64    (50686,)             SCALARS

Plot this mesh with scalars of your choosing. You can apply the same MapdlTheme when plotting as it’s compatible with the grid plotter.

# make interesting scalars
scalars = grid.points[:, 2]  # z coordinates

sbar_kwargs = {"color": "black", "title": "Z Coord"}
grid.plot(
    scalars=scalars,
    show_scalar_bar=True,
    scalar_bar_args=sbar_kwargs,
    show_edges=True,
    theme=my_theme,
)
geometry

This grid can be also saved to disk in the compact cross-platform VTK format and loaded again with pyvista or ParaView.

..code:

>>> grid.save('my_mesh.vtk')
>>> import pyvista
>>> imported_mesh = pyvista.read('my_mesh.vtk')

stop mapdl

mapdl.exit()

Total running time of the script: ( 0 minutes 15.796 seconds)

Gallery generated by Sphinx-Gallery