Note
Go to the end to download the full example code
二进制 MAPDL 文件资源管理器#
本教程将演示如何探索 MAPDL 会话生成的二进制文件内容并提取相关记录。
这些文件包括 APDL 生成的大多数二进制文件(如 .RST
、.FULL
等)。
from ansys.mapdl.core import launch_mapdl
# 将 MAPDL 作为服务启动,并禁用除错误信息之外的所有功能。
from ansys.mapdl.core.examples import vmfiles
mapdl = launch_mapdl()
# mapdl 类下的一个特定属性专门用于 XPL。它基于 APDLMath `*XPL` 命令。
xpl = mapdl.xpl
# 许多命令可通过 xpl 类直接访问:
help(xpl)
Help on ansXpl in module ansys.mapdl.core.xpl object:
class ansXpl(builtins.object)
| ansXpl(mapdl)
|
| ANSYS database explorer.
|
| Examples
| --------
| >>> from ansys.mapdl.core import launch_mapdl
| >>> mapdl = launch_mapdl()
| >>> xpl = mapdl.xpl
|
| Open a mode file and extract a vector.
|
| >>> xpl.open('file.mode')
| >>> vec = xpl.read('MASS')
| >>> vec.asarray()
| array([ 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43,
| 46, 49, 52, 55, 58, 1], dtype=int32)
|
| Methods defined here:
|
| __init__(self, mapdl)
| Initialize the class.
|
| __repr__(self)
| Return repr(self).
|
| close(self)
| Close the MAPDL file after opening.
|
| Returns
| -------
| str
| Response from MAPDL.
|
| Examples
| --------
| >>> xpl.open("file.mode")
| >>> xpl.close()
| ===== ANSYS File Xplorer : Close the file.mode ANSYS File
|
| copy(self, newfile, option='')
| Copy the current opened as a new file.
|
| Parameters
| ----------
| newfile : str
| Name of the new file to create
|
| option: str
| Option.
|
| Examples
| --------
| >>> xpl.copy('tmpfile.full')
| ===== ANSYS File Xplorer : Copy file.full ANSYS file to file tmpfile.full
| >> Remove existing output file tmpfile.full
|
| extract(self, recordname, sets='ALL', asarray=False)
| Import a Matrix/Vector from a MAPDL result file.
|
| At the moment, this only supports reading the displacement vectors from
| a result file.
|
| Parameters
| ----------
| recordname : str
| Record name. Currently only supports the ``"NSL"`` record,
| displacement vectors.
|
| sets : str or int
| Number of sets. Can be ``"ALL"`` or the number of sets to load.
|
| asarray : bool, optional
| Return a :class:`numpy.ndarray` rather than a :class:`AnsMath
| <ansy.math.core.math.AnsMath>`. Default ``False``.
|
| Returns
| -------
| numpy.ndarray or ansys.math.core.math.AnsMath
| A :class:`numpy.ndarray` or :class:`AnsMath
| <ansys.math.core.math.AnsMath>` of the displacement vectors,
| depending on the value of ``asarray``.
|
| Notes
| -----
| This only works on the ``"NSL"`` record of MAPDL result files.
|
| Examples
| --------
| First, open a result file and extract the displacement vectors for all
| sets.
|
| >>> xpl.open("file.rst")
| >>> mat = xpl.extract("NSL")
| >>> mat
| Dense APDLMath Matrix (243, 10)
|
| Convert to a dense numpy array
|
| >>> arr = mat.asarray()
| >>> arr
| array([[-9.30806802e-03, -2.39600770e-02, -5.37856729e-03, ...,
| -5.61188243e-03, -7.17686067e-11, 3.71893252e-03],
| [-1.60960014e-02, 2.00410618e-02, 8.05822565e-03, ...,
| -1.26917511e-02, -5.14133724e-11, -1.38783485e-03],
| [ 2.54040694e-02, 3.91901513e-03, -2.67965796e-03, ...,
| -1.46365178e-02, 8.31735188e-11, -2.33109771e-03],
| ...,
| [-2.80679551e-03, -1.45686692e-02, 8.05466291e-03, ...,
| 5.88196684e-03, 1.72211103e-02, 6.10079082e-03],
| [-7.06675717e-03, 1.30455037e-02, -6.31685295e-03, ...,
| 1.08619340e-02, -1.72211102e-02, 2.52199472e-03],
| [ 2.29726170e-02, 3.54392176e-03, -1.87020162e-03, ...,
| 1.20642736e-02, 2.58299321e-11, 9.14504940e-04]])
|
| goto(self, path)
| Go directly to a new location in the file.
|
| Parameters
| ----------
| path : str
| Absolute path to the new location.
|
| Examples
| --------
| >>> print(xpl.goto('MASS'))
| ===== ANSYS File Xplorer : Go up to top level(s)
| ===== ANSYS File Xplorer : Step into Block MASS
|
| help(self)
| XPL help message.
|
| Examples
| --------
| >>> print(xpl.help())
|
| info(self, recname, option='')
| Gives details on a specific record, or all records (using ``"*"``)
|
| Parameters
| ----------
| recname : str
| Record of interest
|
| option : str
| Options string.
|
| Returns
| -------
| str
| Response from MAPDL.
|
| Examples
| --------
| >>> xpl.open('file.full')
| >>> print(xpl.info('NGPH'))
| ===== ANSYS File Xplorer : Information about Block NGPH
| ::NGPH Size = 6.289 KB
| - Record Size : 81
| - Data type : integer values
|
| json(self)
| Return a JSON representation of the tree or records.
|
| Examples
| --------
| >>> xpl.json()
| {'name': 'FULL',
| 'children': [{'name': 'DOFSBYNOD', 'size': 24},
| {'name': 'BACK', 'size': 336},
| {'name': 'STIFF', 'size': 120132},
| {'name': 'RHS', 'size': 1956},
| {'name': 'DIAGK', 'size': 1956},
| {'name': 'SCLK', 'size': 36},
| {'name': 'NODEEXT', 'size': 32},
| {'name': 'PCGDOFS', 'size': 984},
| {'name': 'BCDOFS', 'size': 984},
| {'name': 'BCVALUES', 'size': 20},
| {'name': 'MASS', 'size': 52020},
| {'name': 'DIAGM', 'size': 1236},
| {'name': 'NGPH', 'size': 6440}]}
|
| list(self, nlev=1)
| List the records at the current level.
|
| Parameters
| ----------
| nlev: int
| Number of levels to recursively explore.
|
| Returns
| -------
| str
| Listing of records from the current level.
|
| Examples
| --------
| Open a full file and list the current records.
|
| >>> xpl.open("file.full")
| >>> xpl.list()
| ===== ANSYS File Xplorer : List Blocks in File file.full
| ::FULL::HEADER Size = 652 B Total Size = 180.297 KB
| ::FULL::DOFSBYNOD Size = 24 B
| ::FULL::BACK Size = 336 B
|
| ::FULL::STIFF::HEADER Size = 117.316 KB
| ::FULL::RHS Size = 1.910 KB
| ::FULL::DIAGK Size = 1.910 KB
| ::FULL::SCLK Size = 1.910 KB
| ::FULL::MRK Size = 984 B
| ::FULL::NODEEXT Size = 336 B
| ::FULL::PCGDOFS Size = 984 B
| ::FULL::BCDOFS Size = 984 B
| ::FULL::BCVALUES Size = 12 B
|
| ::FULL::MASS::HEADER Size = 50.801 KB
| ::FULL::DIAGM Size = 1.910 KB
| ::FULL::NGPH Size = 336 B
|
| open(self, filename, option='')
| Open an MAPDL file to explore.
|
| Parameters
| ----------
| filename : str
| Name of the file to open.
|
| Returns
| -------
| str
| Response from MAPDL.
|
| Examples
| --------
| >>> xpl.open('file.mode')
| ===============================================
| ===== ANSYS File Xplorer ======
| ===============================================
|
| Opening the file.mode ANSYS File
|
| print(self, recname)
| Print values of a given records, or all records (using ``"*"``).
|
| Parameters
| ----------
| recname : str
| Record of interest
|
| option : str
| Options string.
|
| Returns
| -------
| str
| Response from MAPDL.
|
| Examples
| --------
| >>> xpl.open('file.full')
| >>> print(xpl.print('DOFSBYNOD'))
| ===== ANSYS File Xplorer : Print Block DOFSBYNOD
| DOFSBYNOD :
| Size : 3
| 1 2 3
|
| read(self, recordname, asarray=False)
| Read a record and return either an APDL math matrix or an APDL math vector.
|
| Returns
| -------
| ansys.mapdl.AnsMath or ansys.mapdl.AnsVec
| A handle to the APDLMath object.
|
| asarray : bool, optional
| Return a :class:`numpy.ndarray` rather than a :class:`AnsMath
| <ansys.math.core.math.AnsMath>`. Default ``False``.
|
| Examples
| --------
| >>> vec = xpl.read('MASS')
| >>> vec.asarray()
| array([ 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43,
| 46, 49, 52, 55, 58, 1], dtype=int32)
| >>> vec = xpl.read('MASS', asarray=True)
| array([ 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43,
| 46, 49, 52, 55, 58, 1], dtype=int32)
|
| save(self)
| Save the current file, ignoring the marked records.
|
| step(self, where)
| Go down in the tree of records
|
| Parameters
| ----------
| where : str
| Path to follow. This path can be composed of several
| levels, for example ``"BRANCH1::SUBBRANCH2::.."``
|
| Returns
| -------
| str
| Response from MAPDL.
|
| Examples
| --------
| >>> xpl.step('MASS')
| >>> print(xpl.where())
| ===== ANSYS File Xplorer : Display Current Location
| Current Location : FULL::MASS
| File Location : 7644
|
| up(self, nlev=1)
| Go up in the tree.
|
| nlev : int
| Number of levels to recursively go up, or TOP
|
| Examples
| --------
| >>> print(xpl.up())
| ===== ANSYS File Xplorer : Go up to 1 level(s)
| -> Already at the top level. Command is ignored
|
| where(self)
| Returns the current location in the MAPDL file.
|
| Returns
| -------
| str
| String containing the current location.
|
| Examples
| --------
| >>> print(xpl.where())
| ===== ANSYS File Xplorer : Display Current Location
| Current Location : FULL
| File Location : 412
|
| write(self, recordname, vecname)
| Write a given record back to an MAPDL file.
|
| Use the write function at your own risk, you may corrupt an existing
| file by changing the size of a record in the file. This method must be
| used only on a non-compressed file.
|
| Parameters
| ----------
| recordname : str
| Name of the record you want to overwrite. Your position
| in the file must be set accordingly to this record location
| (same as if you want to read it).
|
| vecname : str
| Name of the APDLMath vector you want to write in the MAPDL
| file. Its size must be consistent with the existing record.
|
| Returns
| -------
| str
| Response from MAPDL.
|
| Examples
| --------
| >>> xpl.write('MASS', vecname)
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __annotations__ = {}
Open and explore a file#
首先,您需要打开一个现有文件。我们可以通过运行一个验证手动输入文件来创建一个示例结果文件,然后打开它创建的结果文件。
NOTE: 目前一次只能打开一个文件
# 运行 Verification Manual 1 并打开其创建的结果文件
mapdl.input(vmfiles["vm1"])
print(xpl.open("file.rst"))
===============================================
===== ANSYS File Xplorer ======
===============================================
Opening the file.rst ANSYS File
使用 list 函数,可以列出当前层级的可用记录。
print(xpl.list())
===== ANSYS File Xplorer : List Blocks in File file.rst
::RST::HEADER Size = 332 B Total Size = 319.598 KB
::RST::DOF Size = 24 B
::RST::NOD Size = 28 B
::RST::ELM Size = 24 B
::RST::DSI::HEADER Size = 78.137 KB Total Size = 121.832 KB
::RST::TIM Size = 78.137 KB
::RST::LSP Size = 117.199 KB
::RST::GEO::HEADER Size = 332 B Total Size = 2.031 KB
使用 step
和 up
函数,您可以向下进入树的某个分支,或向上进入树的顶层
xpl.step("GEO")
print(xpl.list())
===== ANSYS File Xplorer : List Blocks in File file.rst
::GEO::HEADER Size = 332 B Total Size = 2.031 KB
::GEO::ETY::HEADER Size = 16 B Total Size = 368 B
::GEO::LOC Size = 136 B
::GEO::EID::HEADER Size = 144 B
::GEO::CENT Size = 108 B
::GEO::SEC::HEADER Size = 16 B Total Size = 92 B
::GEO::MAT::HEADER Size = 816 B Total Size = 848 B
::GEO::NOD Size = 28 B
::GEO::ELM Size = 24 B
显示您在树或记录中的位置:
print(xpl.where())
===== ANSYS File Xplorer : Display Current Location
Current Location : RST::GEO
File Location : 280856
上一级回到顶部,然后列出当前点的记录。
xpl.up()
print(xpl.list())
===== ANSYS File Xplorer : List Blocks in File file.rst
::RST::HEADER Size = 332 B Total Size = 319.598 KB
::RST::DOF Size = 24 B
::RST::NOD Size = 28 B
::RST::ELM Size = 24 B
::RST::DSI::HEADER Size = 78.137 KB Total Size = 121.832 KB
::RST::TIM Size = 78.137 KB
::RST::LSP Size = 117.199 KB
::RST::GEO::HEADER Size = 332 B Total Size = 2.031 KB
Read a record into an APDLMath Vector#
info
方法将提供记录的相关信息(如长度、数据类型等)。
使用 read
方法,可以读取特定记录并填充 APDLMath 对象。
print(xpl.info("DOF"))
v = xpl.read("DOF")
print(v)
===== ANSYS File Xplorer : Information about Block DOF
::DOF Size = 24 B
- Record Size : 3
- Data type : integer values
QDENHQ :
Size : 3
1 2 3
要将该向量转换为 NumPy 数组,需要明确使用 asarray
:
[1 2 3]
读取第一个节点解
# 首先,我们进入第一个 set
#
print(xpl.goto("DSI::SET1"))
print(xpl.list())
===== ANSYS File Xplorer : Go up to top level(s)
===== ANSYS File Xplorer : Step into Block DSI::SET1
===== ANSYS File Xplorer : List Blocks in File file.rst
::SET1::HEADER Size = 812 B Total Size = 43.695 KB
::SET1::DPHEAD Size = 812 B
::SET1::EXT Size = 812 B
::SET1::NSL Size = 68 B
::SET1::RFDOFS Size = 60 B
::SET1::RFVALS Size = 36 B
::SET1::BC::HEADER Size = 172 B Total Size = 348 B
::SET1::ESL::HEADER Size = 40.816 KB
Then we read the Nodal solution vector “NSL” into a numpy array
[ 0.0000000e+00 0.0000000e+00 0.0000000e+00 1.2676506e+30
-8.0000000e-05 1.2676506e+30 1.2676506e+30 -9.0000000e-05
1.2676506e+30 0.0000000e+00 0.0000000e+00 0.0000000e+00]
关闭已打开的文件
print(xpl.close())
===== ANSYS File Xplorer : Close the file.rst ANSYS File
Stop mapdl#
mapdl.exit()
Total running time of the script: (0 minutes 2.751 seconds)