(1)PyAnsys Project(Old)
https://pypi.org/project/pyansys/
pip install pyansys
pip install pyvista
(2)PyAnsys Project(New)
https://docs.pyansys.com/
https://github.com/pyansys
This is the legacy module for reading in binary and ASCII files generated from MAPDL.
This Python module allows you to extract data directly from binary ANSYS v14.5+ files and to display or animate them rapidly using a straightforward API coupled with C libraries based on header files provided by ANSYS.
To use PyAnsys you need to install the applicable packages for your product:
pip install ansys-mapdl-core
pip install pyaedt
pip install ansys-dpf-core
pip install ansys-dpf-post
pip install ansys-mapdl-reader
The test environment here is :Win10 x64 position ,Python 3.9.7, VsCode Code editor .
pip install ansys-mapdl-core
from ansys.mapdl.core import launch_mapdl
mapdl = launch_mapdl()
print(mapdl)
Cached ANSYS executable not found
Enter location of ANSYS executable:
Tips : The output information indicates that the functions of this sub library need to be installed on the computer ANSYS Software .
pip install pyaedt
pip install ansys-dpf-core
ValueError: Unable to automatically locate the Ansys path for version 221.Manually enter one when starting the server or set it as the environment variable “ANSYS_PATH”
Tips : The output information indicates that the functions of this sub library need to be installed on the computer ANSYS Software .
pip install ansys-dpf-post
ValueError: Unable to automatically locate the Ansys path for version 221.Manually enter one when starting the server or set it as the environment variable “ANSYS_PATH”
Tips : The output information indicates that the functions of this sub library need to be installed on the computer ANSYS Software .
pip install ansys-mapdl-reader
The test code is as follows :
from ansys.mapdl import reader as pymapdl_reader
from ansys.mapdl.reader import examples
# Sample result file
rstfile = examples.rstfile
# Create result object by loading the result file
result = pymapdl_reader.read_binary(rstfile)
result.plot_nodal_solution(0)
No error was reported during operation !!!
Tips : The output information explains the function of this sub Library Unwanted On the computer ANSYS Software !!!
The following chapters use ansys-mapdl-reader and pyansys Two sub libraries test related functions .
ANSYS archive files containing solid elements (both legacy and modern), can be loaded using Archive and then converted to a vtk object.
from ansys.mapdl import reader as pymapdl_reader
from ansys.mapdl.reader import examples
import pyansys
# Sample *.cdb
filename = examples.hexarchivefile
# Read ansys archive file
archive = pyansys.Archive(filename)
# Print raw data from cdb
# for key in archive.raw:
# print("%s : %s" % (key, archive.raw[key]))
print(archive.nodes)
# Create a vtk unstructured grid from the raw data and plot it
print(dir(archive))
grid = archive._parse_vtk(force_linear=True)
grid.plot(color='w', show_edges=True)
# write this as a vtk xml file
grid.save('hex.vtu')
# or as a vtk binary
grid.save('hex.vtk')
You can then load this vtk file using pyvista or another program that uses VTK.
# Load this from vtk
import pyvista as pv
grid = pv.UnstructuredGrid('hex.vtu')
grid.plot()
This example reads in binary results from a modal analysis of a beam from MAPDL.
# Load the reader from pyansys
from ansys.mapdl import reader as pymapdl_reader
from ansys.mapdl.reader import examples
import pyansys
# Sample result file
rstfile = examples.rstfile
# Create result object by loading the result file
result = pyansys.read_binary(rstfile)
# Beam natural frequencies
freqs = result.time_values
print(freqs)
Get the 1st bending mode shape. Results are ordered based on the sorted node numbering. Note that results are zero indexed
nnum, disp = result.nodal_solution(0)
print("nnum=", nnum)
print("disp=", disp)
# Plot the displacement of Mode 0 in the x direction
result.plot_nodal_solution(0, 'x', label='Displacement')
First, get the camera position from an interactive plot:
cpos=None
result.plot_nodal_solution(0, 'x', label='Displacement', cpos=cpos,
screenshot='hexbeam_disp.png',
window_size=[800, 600], interactive=False)
Stress can be plotted as well using the below code. The nodal stress is computed in the same manner that Ansys uses by to determine the stress at each node by averaging the stress evaluated at that node for all attached elements. For now, only component stresses can be displayed.
Please select from the following: [‘X’, ‘Y’, ‘Z’, ‘XY’, ‘YZ’, ‘XZ’]
# Display node averaged stress in x direction for result 6
result.plot_nodal_stress(5, 'X')
from ansys.mapdl import reader as pymapdl_reader
result = pymapdl_reader.read_binary('d:\\demo.rst')
print(result)
print(result.mesh)
print(result.n_results)
print(result.nsets)
print(result.version)
print(result._geometry_header)
print(result._resultheader)
freqs = result.time_values
print(freqs)
Returns the DOF solution for each node in the global cartesian coordinate system or nodal coordinate system.Solution may be nodal temperatures or nodal displacements depending on the type of the solution.
nnum, disp = result.nodal_solution(0)
print("nnum=", nnum)
print("disp=", disp)
nnum, disp = result.nodal_displacement(0)
print("nnum=", nnum)
print("disp=", disp)
Return an informative dictionary of solution data for a result.
data = result.solution_info(0)
print(data)
Return a list of degrees of freedom for a given result number.
data = result.result_dof(0)
print(data)
Plots the nodal solution.
Defaults to “NORM” for a structural displacement result, and “TEMP” for a thermal result.
result.plot_nodal_solution(0)
result.plot_nodal_solution(59)
result.plot_nodal_solution(10, background='g', show_edges=True)
enum, edata, enode = result.element_solution_data(0, datatype='ENS')
print(enum)
print(edata)
print(enode)
[ 1 2 3 … 36953 36954 36955]
If you think the method or code is a little useful , You can praise the author ;╮( ̄▽ ̄)╭
If you don't feel good about the method or code //(ㄒoㄒ)//, Just leave a message in the comments , The author continues to improve .o_O???
Thank you, children's shoes ( ´ ▽´ )ノ ( ´ ▽´)っ!!!