Chapter 2: Run Python code within Mechanical APDL

Use the new APDL process control commands, *PYTHON and *ENDPY, to add Python code directly in a Mechanical APDL input listing. These commands define a block of content that is interpreted as Python commands rather than Mechanical APDL commands. *PYTHON initiates the Python block and *ENDPY terminates it.

You can add multiple *PYTHON blocks in a Mechanical APDL input listing, and any local Python variables defined in one block can be accessed from another block. If an exception is raised from within a *PYTHON block, it causes an error in Mechanical APDL, which may lead to program termination.

The following topics are available:

Integration with PyMAPDL

*PYTHON enables tighter integration between PyMAPDL and Mechanical APDL. PyMAPDL is an open source library that makes it easy to integrate the simulation capabilities of the Mechanical APDL multi-physics solver directly into novel applications. With PyMAPDL, you can create a Python program that runs outside of the solver and interacts with it using inter-process communication (IPC). Developers can use PyMAPDL to combine the capabilities of Mechanical APDL with the extensive ecosystem of Python libraries. See PyMAPDL documentation for details.

Introducing *PYTHON

*PYTHON offers an alternative interface to Mechanical APDL, building on the capabilities of PyMAPDL. Unlike PyMAPDL, *PYTHON runs Python code directly inside of the Mechanical APDL process. MAPDL commands that have been wrapped pythonically via PyMAPDL (listed here:https://mapdl.docs.pyansys.com/version/stable/mapdl_commands/index.html ) can be used in a *PYTHON block in addition to all Python commands.

*PYTHON is an APDL process control commands, much like *DO or *IF. It defines a block within a Mechanical APDL deck that ends with a *ENDPY that is interpreted as Python rather than APDL.

See the following examples of *PYTHON blocks.

/COM,*PYTHON example below
*PYTHON
mapdl.prep7()
mapdl.view(-1,-2,-3)
mapdl.et(1,30)
mapdl.et(2,181,,,2)
*ENDPY
r,1
r,2

Within this block, the mapdl variable is an instance of the MAPDL object, which is the main object used by pyMAPDL scripts. You can also use the dpf variable, which is an instance of the ansys.dpf.core module. Another example that uses the NumPy library follows.

/COM, *PYTHON example below
*PYTHON
import numpy as np
import itertools

vertices = np.array([
    [5, 12],
    [8, 18],
    [13, 14],
    [11, 6],
    [4, 6],
])
nodes = []
for position in vertices:
    nodes.append(mapdl.n("", *position))

lines = []
for i1, i2 in itertools.pairwise(nodes):
    lines.append(mapdl.l(i1, i2))
lines.append(mapdl.l(0, len(vertices) - 1))
area_id = mapdl.a(*lines)
*ENDPY

*GET, NODENUMBER, NODE, 0, NUM, MAX
*GET, LINENUMBER, LINE, 0, NUM, MAX
*GET, AREANUMBER, AREA, 0, NUM, MAX
N,NODENUMBER+1
N,NODENUMBER+2,0,0,10
L,LINENUMBER+1,NODENUMBER+1,NODENUMBER+2
EXTD,LINENUMBER+1,AREANUMBER

Python language version

*PYTHON uses the 3.10 version of Python that is distributed with Mechanical APDL, in the location v242/commonfiles/CPython. For documentation of this version of Python, see https://docs.python.org/3.10/.

*PYTHON in Distributed-Memory Parallel (DMP) Mode

When running in DMP mode, the Python code inside the *PYTHON block only executes on the head compute node.

Using Python Packages

By default, some thirdparty Python libraries, such as numpy, are available for use in a *PYTHON block.