PyAnsys Cheat Sheets
PyAnsys Cheat Sheets
version:0.65.0 (stable)
Launching PyMAPDL Converting an existing APDL script to PyMAPDL format. Post-Processing Class
To launch PyMAPDL instance locally and exit it. This class is used for plotting and saving results to NumPy
inputfile='ansys_inputfile.inp'
arrays.
# To launch an instance pyscript='pyscript.py'
from ansys.mapdl.core import launch_mapdl mapdl.convert_script(inputfile, pyscript) mapdl.post1()
mapdl=launch_mapdl() mapdl.set(1, 2)
# To exit the instance # Plot the nodal equivalent stress
mapdl.exit()
MAPDL Class mapdl.post_processing.plot_nodal_eqv_stress()
Load a table from Python to MAPDL # Save nodal eqv. stresses to a Python array
To specify a jobname, number of processors, and working nod_eqv_stress=
directory. mapdl.load_table(name,array,var1='', var2='',
var3='', csysid='') mapdl.post_processing.nodal_eqv_stress()
# Plot contour legend using dictionary
jname='user_jobname'
To access from or write parameters to MAPDL database. mapdl.allsel()
path='<path of directory>'
sbar_kwargs={"color": "black",
mapdl=launch_mapdl(nproc=2, run_location=path,
# Save a parameter to a NumPy array nparray "title": "Equivalent Stress (psi)",
jobname=jname)
nparray=mapdl.parameters['displ_load'] "vertical": False,"n_labels": 6}
To connect to an existing instance of MAPDL at IP # Create a parameter from a NumPy array nparray mapdl.post_processing.plot_nodal_eqv_stress(
192.168.1.30 and port 50001. mapdl.parameters['exp_disp']=nparray cpos='xy', background='white',
edge_color='black', show_edges=True,
mapdl=launch_mapdl To access information using *GET and *VGET directly to scalar bar_args=sbar_kwargs, n_colors=9)
(start_instance=False, NumPy arrays.
ip='192.168.1.30', port=50001)
# Runs *GET command and returns a Python value
mapdl.get_value(entity='', entnum='', Plotting Class
To create and exit a pool of instances.
item1='', it1num='', Plotting is interpolated with PyVista by saving the resulting
# To create a pool of 10 instances item2='', it2num='', **kwargs) stress and storing wtihin the underlying UnstructuredGrid.
from ansys.mapdl.core import LocalMapdlPool
# Runs *VGET command and returns a Python array pl=pyvista.Plotter()
pool=mapdl.LocalMapdlPool(10)
mapdl.get_array(entity='', entnum='', pl0=mapdl.post_processing.plot_nodal_stress(
# To exit the pool
item1='', it1num='', item2='', return_plotter=True)
pool.exit()
it2num='', kloop='', **kwargs) pl.add(pl0.mesh)
pl.show()
PyMAPDL Language
PyMAPDL commands are Python statements that act as a Mesh Class
# Plot the currently selected elements
wrapper for APDL commands. For instance, ESEL, s, type, 1 is Store the finite element mesh as a VTK UnstructuredGrid
mapdl.eplot(show_node_numbering, vtk)
translated as data object.
# Plot the selected volumes
mapdl.esel('s', 'type', vmin=1) grid=mapdl.mesh.grid mapdl.vplot(nv1, nv2, ninc, degen, scale, ...)
# Display the selected areas
Commands that start with * or / have those characters Save element & node numbers to Python arrays. mapdl.aplot(na1, na2, ninc, degen, scale, ...)
removed. # Display the selected lines without
# Array of nodal coordinates # MAPDL plot symbols
mapdl.prep7() # /PREP7 nodes=mapdl.mesh.nodes mapdl.lplot(vtk=True, cpos='xy', line_width=10)
mapdl.get() # *GET # Save png file of line plot with MAPDL
# Save node numbers of selected nodes to array # coordinate symbol
In cases where removing * or / will cause conflict with other node_num=mapdl.mesh.nnum mapdl.psymb('CS', 1)
commands, a prefix "slash" or "star" is added. # Save node numbers of all nodes to array mapdl.lplot(vtk=False)
node_num_all=mapdl.mesh.nnum_all
mapdl.solu() # SOLU
mapdl.slashsolu() # /SOLU # Element numbs. of currently selected elements References from PyMAPDL Documentation
elem_num=mapdl.mesh.enum • Getting Started
mapdl.vget() # VGET # All element numbers incl. those not selected
mapdl.starvget() # *VGET elem_num_all=mapdl.mesh.enum_all • MAPDL Commands
• API Reference
The solver object contains attributes such as file, setup, Define boundary conditions Post-Processing
solution, and results, which are also instances of settings The examples in this section show how you use Solver PyFluent allows you to post-process data with results object.
objects. settings objects to define boundary conditions. The following example shows you how to create and display
contours on a plane.
Import mesh in launched session solver.setup.boundary_conditions.velocity_inlet["cold-
The following examples show how to read the available mesh inlet"].vmag = { solver.results.graphics.contour["contour"] = {}
file in Fluent Session. "option": "constant or expression", solver.results.graphics.contour["contour"].
"constant": 0.4, print_state()
import_filename = 'example_file.msh.h5'
} solver.results.graphics.contour["contour"].field = "
solver.file.read(file_type="case", file_name=
solver.setup.boundary_conditions.velocity_inlet[ temperature"
import_filename)
"cold-inlet" solver.results.graphics.contour["contour"].
There are other specific APIs available for reading case files ].ke_spec = "Intensity and Hydraulic Diameter" surfaces_list = [
and reading case-data files. solver.setup.boundary_conditions.velocity_inlet["cold- "symmetry-xyplane",
inlet"].turb_intensity = 5 ]
# e.g., read_case(), read_case_data() solver.setup.boundary_conditions.velocity_inlet[
"cold-inlet"
import_filename = 'example_file.cas.h5' ].turb_hydraulic_diam = "4 [in]"
solver.file.read_case(file_type="case", file_name= solver.setup.boundary_conditions.velocity_inlet["cold-
Temperature Contour
import_filename) inlet"].t = {
"option": "constant or expression",
"constant": 293.15,
Enable heat transfer physics }
The following examples show how to enable heat transfer by
activating the energy equation.
Modify cell zone conditions
solver.setup.models.energy.enabled = True
The examples in this section show how you use Solver
Accessing the object state with using pprint settings objects to modify cell zone conditions.