相关文章汇总如下:
https://pypi.org/project/pyansys/
pip install pyansys
pip install pyvista
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
这里测试环境是:
这个项目最初是作为一个单独的包开始的pyansys,并且已经扩展到五个主要包:
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:
提示:输出信息说明这个子库的功能需要电脑上安装ANSYS软件。
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”
提示:输出信息说明这个子库的功能需要电脑上安装ANSYS软件。
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”
提示:输出信息说明这个子库的功能需要电脑上安装ANSYS软件。
This module will likely change or be depreciated in the future.
You are encouraged to use the new Data Processing Framework (DPF) modules at DPF-Core and DPF-Post as they provide a modern interface to ANSYS result files using a client/server interface using the same software used within ANSYS Workbench, but via a Python client.
pip install ansys-mapdl-reader
测试代码如下:
import pyansys
from pyansys import examples
rst = pyansys.read_binary(examples.rstfile)
freqs = rst.time_values
print(freqs)
print(rst.mesh)
print(rst.mesh.nnum)
print(rst.mesh.enum)
rst.plot_nodal_solution(0)
rst.plot_nodal_solution(1)
rst.plot_nodal_solution(2)
rst.plot_nodal_solution(3)
rst.plot_nodal_solution(4)
rst.plot_nodal_solution(5)
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)
result.plot_nodal_solution(1)
result.plot_nodal_solution(2)
result.plot_nodal_solution(3)
result.plot_nodal_solution(4)
result.plot_nodal_solution(5)
运行没有报错!!!
提示:输出信息说明这个子库的功能不需要电脑上安装ANSYS软件!!!
(1)这是用于读取从 MAPDL 生成的二进制和 ASCII 文件的遗留模块。
这个 Python 模块允许您直接从二进制 ANSYS v14.5+ 文件中提取数据,并使用简单的 API 以及基于 ANSYS 提供的头文件的 C 库快速显示或动画化它们。
(2)该模块将来可能会更改或废弃。
我们鼓励您在DPF-Core和 DPF-Post上查看新的数据处理框架 (DPF) 模块,因为它们使用客户端/服务器接口为 ANSYS 结果文件提供现代接口使用 ANSYS Workbench 中使用的相同软件,但通过 Python 客户端。
下面章节就使用ansys-mapdl-reader子库测试相关功能。
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.
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)
# 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')
result.animate_nodal_solution(0, loop=False, movie_filename='result.gif',
background='grey', displacement_factor=0.01, show_edges=True,
add_text=True,
n_frames=30)
import pyvista
import numpy as np
from ansys.mapdl import reader as pymapdl_reader
from ansys.mapdl.reader import examples
# Download an example shaft modal analysis result file
shaft = examples.download_shaft_modal()
print('shaft.mesh:\n', shaft.mesh)
print('-'*79)
print('shaft.grid:\n', shaft.grid)
## plot one
# shaft.grid.plot(color='w', smooth_shading=True)
## plot two
x_scalars = shaft.grid.points[:, 0]
shaft.grid.plot(scalars=x_scalars, smooth_shading=True)
import pyvista
import numpy as np
from ansys.mapdl import reader as pymapdl_reader
from ansys.mapdl.reader import examples
pontoon = examples.download_pontoon()
nnum, strain = pontoon.nodal_elastic_strain(0)
scalars = strain[:, 0]
scalars[:2000] = np.nan # here, we simulate unknown values
pontoon.grid.plot(scalars=scalars, show_edges=True, lighting=False)
圆柱节点应力:可视化径向上的节点应力。这相当于在 MAPDL 中将结果坐标系设置为圆柱体。
from ansys.mapdl.reader import examples
rst = examples.download_corner_pipe()
# obtain the cylindrical_nodal_stress
nnum, stress = rst.cylindrical_nodal_stress(0)
print(stress)
# contains results for each node in following directions
# R, THETA, Z, RTHETA, THETAZ, and RZ
print(stress.shape)
## 绘制径向上的圆柱节点应力
# _ = rst.plot_cylindrical_nodal_stress(0, 'R', show_edges=True, show_axes=True)
## 在 theta 方向绘制圆柱节点应力
# _ = rst.plot_cylindrical_nodal_stress(0, 'THETA', show_edges=True, show_axes=True,
# add_text=False)
## 在“X”方向绘制笛卡尔应力
_ = rst.plot_nodal_stress(0, 'X', show_edges=True, show_axes=True)
# sphinx_gallery_thumbnail_number = 6
from ansys.mapdl.reader import examples
# Download an example shaft modal analysis result file
shaft = examples.download_shaft_modal()
print(shaft.mesh)
print(shaft.mesh._grid)
## 绘制节点组件
# cpos = shaft.plot()
cpos = [(-115.35773008378118, 285.36602704380107, -393.9029392590675),
(126.12852038381345, 0.2179228023931401, 5.236408799851887),
(0.37246222812978824, 0.8468424028124546, 0.37964435122285495)]
## 将节点组件绘制为线框
# shaft.plot(element_components=['SHAFT_MESH'], cpos=cpos, style='wireframe',
# lighting=False)
## 绘制带有边缘和蓝色的轴
# shaft.plot(show_edges=True, color='cyan')
## 绘制没有照明但有边缘和蓝色的轴
# shaft.plot(lighting=False, show_edges=True, color='cyan')
## 使用“bwr”颜色图绘制没有等高线的振型
# shaft.plot_nodal_solution(9, element_components=['SHAFT_MESH'],
# show_displacement=True, cmap='bwr',
# displacement_factor=0.3, stitle=None,
# overlay_wireframe=True, cpos=cpos)
## 绘制带有轮廓和默认颜色图的模式形状
# shaft.plot_nodal_solution(1, element_components=['SHAFT_MESH'],
# n_colors=10, show_displacement=True,
# displacement_factor=1, stitle=None,
# overlay_wireframe=True, cpos=cpos)
## 为轴组件的模式设置动画
shaft.animate_nodal_solution(5, element_components='SHAFT_MESH',
comp='norm', displacement_factor=1,
show_edges=True, cpos=cpos,
loop=False, movie_filename='demo.gif',
n_frames=30)
from ansys.mapdl.reader import examples
vm33 = examples.download_verification_result(33)
# get nodal thermal strain for result set 1
nnum, tstrain = vm33.nodal_thermal_strain(0)
# plot nodal thermal strain for result set 11 in the X direction
# vm33.plot_nodal_thermal_strain(10, 'X', show_edges=True,
# lighting=True, cmap='bwr', show_axes=True)
## 绘制等高线
# Disable lighting and set number of colors to 10 to make an MAPDL-like plot
#vm33.plot_nodal_thermal_strain(10, 'X', show_edges=True, n_colors=10,
# interpolate_before_map=True,
# lighting=False, show_axes=True)
vm33.animate_nodal_solution(10, loop=False, movie_filename='vm33.gif',
background='grey', comp='norm', displacement_factor=1, show_edges=True,
add_text=True,
n_frames=30)
# download the pontoon example
from ansys.mapdl.reader import examples
pontoon = examples.download_pontoon()
print(pontoon)
## 绘制节点位移
# pontoon.plot_nodal_solution(0, show_displacement=True, displacement_factor=100000)
## 打印可用的结果类型
pontoon.available_results
## 绘制壳元素
# pontoon.plot()
## 绘制弹性应变并显示夸张的位移
#pontoon.plot_nodal_elastic_strain(0, 'eqv', show_displacement=True,
# displacement_factor=100000,
# overlay_wireframe=True,
# lighting=False,
# add_text=False,
# show_edges=True)
pontoon.animate_nodal_solution(0, loop=False, movie_filename='pontoon.gif',
background='grey', displacement_factor=100000,
add_text=True,
n_frames=20)
从循环模型分析中了解节点直径。
此示例说明如何从来自模态分析的单叶片扇区和多叶片扇区的 MAPDL 结果文件中解释循环分析中的模态。
import numpy as np
from ansys.mapdl.reader import examples
rotor = examples.download_academic_rotor_result()
print(rotor)
# _ = rotor.plot_sectors(cpos='xy', stitle='Sector', smooth_shading=True, cmap='bwr')
## 您可以使用 MAPDL 的基于 1 的索引(加载步骤,子步骤)来引用结果集。
# _ = rotor.plot_nodal_displacement((2, 2), comp='norm', cpos='xy')
## 您可以使用累积索引来参考结果。
# _ = rotor.plot_nodal_displacement(10, comp='norm', cpos='xy')
_ = rotor.animate_nodal_displacement((3, 1), displacement_factor=0.03,
n_frames=30, show_axes=False, background='grey',
loop=False, add_text=True,
movie_filename='rotor1.gif')
循环模态分析中的应力和应变。
此示例说明如何从循环模态分析中提取应变和应力。
from ansys.mapdl.reader import examples
rotor = examples.download_academic_rotor_result()
print(rotor)
# _ = rotor.plot_nodal_displacement((2, 2), 'x', cpos='xy')
# nnum, strain = rotor.nodal_elastic_strain(3, full_rotor=True)
# _ = rotor.plot_nodal_elastic_strain((5, 2), 'Z', show_displacement=True,
# displacement_factor=0.01)
# _ = rotor.plot_nodal_stress((5, 1), 'Z', show_displacement=True,
# displacement_factor=0.01)
#_ = rotor.plot_principal_nodal_stress((5, 2), 'SEQV', show_displacement=True,
# displacement_factor=0.01)
_ = rotor.animate_nodal_displacement((2, 2), displacement_factor=0.03,
n_frames=50, show_axes=False, background='grey',
loop=False, add_text=True,
movie_filename='rotor2.gif')
可视化和动画完整的循环模型。该模型基于 jetcat 转子。
# sphinx_gallery_thumbnail_number = 2
from ansys.mapdl.reader import examples
rotor = examples.download_sector_modal()
print(rotor)
# rotor.plot_sectors(cpos='xy', smooth_shading=True)
# rotor.plot()
# rotor.plot_nodal_displacement(20, show_displacement=True,
# displacement_factor=0.001,
# overlay_wireframe=True) # same as (2, 4)
rotor.animate_nodal_solution(20, loop=False, movie_filename='rotor_mode.gif',
background='w', displacement_factor=0.001,
add_text=False,
n_frames=30)
(1)Reads ANSYS-written binary files:
- Jobname.RST: Result file from structural analysis
- Jobname.RTH: Result file from a thermal analysis
- Jobname.EMAT: Stores data related to element matrices
- Jobname.FULL: Stores the full stiffness-mass matrix
(2)Examples
--------
>>> import pyansys
>>> result = pyansys.read_binary('file.rst')
>>> result = pyansys.read_binary('file.rst')
>>> full_file = pyansys.read_binary('file.full')
>>> emat_file = pyansys.read_binary('file.emat')
from ansys.mapdl import reader as pymapdl_reader
result = pymapdl_reader.read_binary('d:\\demo.rst')
print(result)
print(result.n_results)
print(result.nsets)
print(result.version)
(3)方法3:
solution_type: str, optional
The solution type. Must be either nodal displacements('NSL'
), nodal velocities ('VEL'
) or nodal
accelerations ('ACC'
).
# 节点的位移数据
data = result.nodal_time_history("NSL")
print(data)
将结果数据存到本地CSV文件中:
import numpy as np
arr = np.column_stack((nnum, disp))
np.savetxt('d:\\myfile.csv', arr, delimiter=',')
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(10)
result.plot_nodal_solution(20)
result.plot_nodal_solution(30)
result.plot_nodal_solution(11, background='g', show_edges=True)
result.plot_nodal_solution(22, background='y', show_edges=True)
Element data type to retrieve:
- EMS: misc. data
- ENF: nodal forces
- ENS: nodal stresses
- ENG: volume and energies
- EGR: nodal gradients
- EEL: elastic strains
- EPL: plastic strains
- ECR: creep strains
- ETH: thermal strains
- EUL: euler angles
- EFX: nodal fluxes
- ELF: local forces
- EMN: misc. non-sum values
- ECD: element current densities
- ENL: nodal nonlinear data
- EHC: calculated heat generations
- EPT: element temperatures
- ESF: element surface stresses
- EDI: diffusion strains
- ETB: ETABLE items
- ECT: contact data
- EXY: integration point locations
- EBA: back stresses
- ESV: state variables
- MNL: material nonlinear record
如果您觉得该方法或代码有一点点用处,可以给作者点个赞,或打赏杯咖啡;
╮( ̄▽ ̄)╭
如果您感觉方法或代码不咋地
//(ㄒoㄒ)//,就在评论处留言,作者继续改进;
o_O???
如果您需要相关功能的代码定制化开发,可以留言私信作者;
(✿◡‿◡)
感谢各位大佬童鞋们的支持!
( ´ ▽´ )ノ ( ´ ▽´)っ!!!