A-A+

PyAnsys入门:PyMAPDL在Python环境中的导入和运行

2023年12月04日 CAE 阅读 347 views 次

1、PyMAPDL介绍

PyMAPDL是PyAnsys中的一个模块,用来通过Python控制ANSYS Mechanical APDL模块进行建模和仿真。其核心的软件包是ansys-mapdl-core类库,通过gPRC协议和ANSYS Mechanical APDL之间进行数据通信。详细的调用和通信方式如下图所示。

2、PyMAPDL模块在Python环境中的导入

要在Python环境中使用PyMAPDL模块,首先要确保当前的Python工作环境中安装了PyAnsys的PyMAPDL模块。详细的安装过程可参考文章《用强大的Python控制你的ANSYS——PyAnsys介绍和安装》。可通过以下代码导入其核心库ansys-mapdl-core。

from ansys.mapdl.core import launch_mapdlmapdl = launch_mapdl()print(mapdl) 

如果安装正常会打印出ANSYS的Product信息和MAPDL的版本信息,如下所示。

Product: Ansys Mechanical EnterpriseMAPDL Version: 23.1ansys.mapdl Version: 0.63.2

在导入时可能会遇到如下错误提示:

Enter the location of an ANSYS executable (ansysXXX):
Cached ANSYS executable not found
You are about to enter manually the path of the ANSYS MAPDL executable (ansysXXX, where XXX is the versionThis file is very likely to contained in path ending in 'vXXX/ansys/bin/ansysXXX', but it is not required.
If you experience problems with the input path you can overwrite the configuration file by typing:
>>> from ansys.mapdl.core.launcher import save_ansys_path
>>> save_ansys_path('/new/path/to/executable

如果是在Jupyter等Markdown编辑器中,还会给出如下所示输入框。

此时只需在输入框中输入ANSYS带版本号的可执行文件的目录位置即可。也可通过如下所示Python语句进行设置。(假设ANSYS版本为2023,安装在D:\ANSYS Inc目录中)

from ansys.mapdl.core.launcher import save_ansys_pathsave_ansys_path('D:/ANSYS Inc/v231/ANSYS/bin/winx64/ansys231.exe')

3、PyMAPDL的命令函数执行方式

PyMAPDL有两种方式与ANSYS Mechanical APDL进行交互。一种是通过run函数向MAPDL发送APDL命令。例如创建一个面,可通过如下语句实现:

mapdl.run("/PREP7")mapdl.run("K, 1, 0, 0, 0")mapdl.run("K, 2, 1, 0, 0")mapdl.run("K, 3, 1, 1, 0")mapdl.run("K, 4, 0, 1, 0")mapdl.run("L, 1, 2")mapdl.run("L, 2, 3")mapdl.run("L, 3, 4")mapdl.run("L, 4, 1")mapdl.run("AL, 1, 2, 3, 4")

输出创建结果:

mapdl.lplot(cpos="xy", line_width=3, font_size=26, color_lines=True, background="w")

另一种方式是通过MAPDL的内部函数来实现,如下所示两个语句是等价的。

mapdl.k(1, 0, 0, 0)mapdl.run("K, 1, 0, 0, 0")

推荐使用第二种方式进行操作,特别是当使用Python的其他模块库进行混合编程时,通过MAPDL的内部函数来与Mechanical APDL进行交互优势非常明显。例如通过numpy数组快速创建点:

# make 10 random keypoints in MAPDLpoints = np.random.random((10, 3))for i, (x, y, z) in enumerate(points): mapdl.k(i + 1, x, y, z)

4、PyMAPDL函数介绍

所有通过APDL语言可以实现的功能几乎都可以通过MAPDL中对应的函数来实现。例如查看网格信息:

此文章为微信公众号原创文章,查看全部文章内容请关注微信公众号“数字化设计CAX联盟”,回复数字:8500查看。
个人公众号“数字化设计CAX联盟”,欢迎关注,共同交流
为您推荐:

1 条留言  访客:0 条  博主:0 条   引用: 1 条

来自外部的引用: 1 条

  • PyAnsys之PyMAPDL:在Python中连接Ansys Mechanical APDL | 坐倚北风

给我留言

© 坐倚北风 版权所有 严禁镜像复制 苏ICP备15034888号. 基于 Ality 主题定制 AliCMS
联系邮箱:leanwind@163.con,微信公众号:数字化设计CAX联盟

用户登录

分享到: