程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[ANSYS] Python parses the ANSYS result file (pyansys Library)

編輯:Python

List of articles

  • 1、 download PyAnsys library
  • 2、 install PyAnsys
    • 2.1 PyMAPDL
    • 2.2 PyAEDT
    • 2.3 PyDPF-Core
    • 2.4 PyDPF-Post
    • 2.5 Legacy PyMAPDL Reader
  • 3、 Official website test case
    • 3.1 Example:Loading and Plotting an Ansys Archive File
    • 3.2 Example:loading vtk file by using pyvista
    • 3.3 Example:Loading the Result File
    • 3.4 Example:Listing Nodal Results
    • 3.5 Example:Plotting Nodal Results(Solution)
    • 3.6 Example:Plotting Nodal Results(Stress)
  • 4、 Personal test cases
    • 4.1 load rst Results file
    • 4.2 Output general information
    • 4.3 Load frequency data is output in a list
    • 4.4 Node data is output in a list
    • 4.5 The node data is output in the form of drawing
    • 4.6 The unit data is output in a list
  • Postscript

1、 download PyAnsys library

(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:

  • MAPDL:
pip install ansys-mapdl-core
  • AEDT:
pip install pyaedt
  • MAPDL Post-Processing:
pip install ansys-dpf-core
pip install ansys-dpf-post
pip install ansys-mapdl-reader

2、 install PyAnsys

The test environment here is :Win10 x64 position ,Python 3.9.7, VsCode Code editor .

2.1 PyMAPDL

  • Install this package :
pip install ansys-mapdl-core
  • Directly from Python And MAPDL Process communication .
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 .

2.2 PyAEDT

  • Install this package :
pip install pyaedt

2.3 PyDPF-Core

  • Install this package :
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 .

2.4 PyDPF-Post

  • Install this package :
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 .

2.5 Legacy PyMAPDL Reader

  • Install this package :
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 .

3、 Official website test case

3.1 Example:Loading and Plotting an Ansys Archive File

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')

3.2 Example:loading vtk file by using pyvista

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()

3.3 Example:Loading the Result File

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)

3.4 Example:Listing Nodal Results

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)

3.5 Example:Plotting Nodal Results(Solution)

# 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)

3.6 Example:Plotting Nodal Results(Stress)

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')

4、 Personal test cases

4.1 load rst Results file

from ansys.mapdl import reader as pymapdl_reader
result = pymapdl_reader.read_binary('d:\\demo.rst')
print(result)

4.2 Output general information

print(result.mesh)

print(result.n_results)
print(result.nsets)
print(result.version)

print(result._geometry_header)

print(result._resultheader)

4.3 Load frequency data is output in a list

freqs = result.time_values
print(freqs)

4.4 Node data is output in a list

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.

  • Method 1:
nnum, disp = result.nodal_solution(0)
print("nnum=", nnum)
print("disp=", disp)
  • Method 2:
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)

4.5 The node data is output in the form of drawing

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)

4.6 The unit data is output in a list

enum, edata, enode = result.element_solution_data(0, datatype='ENS')
print(enum)
print(edata)
print(enode)

[ 1 2 3 … 36953 36954 36955]

Postscript

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 ( ´ ▽´ )ノ ( ´ ▽´)っ!!!


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved