入门指南
简介
abqpy
是一个 Python 包,提供了一些类型提示,用于快速编写你的 Abaqus/Python 脚本,即便不打开 Abaqus/CAE。它还提供了一些简单的 API 用于调用 Abaqus 命令执行你的 Python 脚本,以便使用 Python 脚本快速构建 Abaqus 模型,并提交作业给 Abaqus 进行计算,并从计算结果中提取数据。
安装
Make sure and
are installed on your computer before installing
abqpy
.
你可以使用以下命令安装 abqpy
。
pip install abqpy==2023.* # recommended pip install abqpy==2023.5.6 pip install abqpy # with jupyter notebook support pip install abqpy[jupyter]==2023.* # recommended pip install abqpy[jupyter]==2023.5.6 pip install abqpy[jupyter]
pip install git+https://github.com/haiiliin/abqpy@2023 pip install ipynbname nbconvert # with jupyter notebook support
备注
建议您安装相应版本的 Abaqus 和 abqpy
,以避免兼容性问题。
两个 Python 解释器
在我们进一步讨论之前,我们有必要了解两个 Python 解释器。
当我们使用 Abaqus/CAE 图形用户界面(GUI)创建模型并可视化结果时,命令由 Abaqus/CAE 在每次操作后在内部发出。这些命令反映您创建的几何图形以及从每个对话框中选择的选项和设置。GUI 使用名为 Python 的面向对象编程语言生成命令。GUI 发出的命令将发送到 Abaqus/CAE 内核。内核解释命令,并使用选项和设置来创建模型的内部表示。内核是 Abaqus/CAE 背后的大脑。GUI 是用户和内核之间的接口。
总之,Abaqus 使用 Python 语言与 Abaqus 内核进行交互,所有可以在 Abaqus/CAE 中 完成的事情,也可以使用 Python 脚本完成。Abaqus 已经安装了一个 Python 解释器,以便 Abaqus/CAE 可以使用它与 Abaqus 内核进行交互。
由于某些原因,我们不能直接使用 Abaqus 内部的 Python 解释器来构建 Abaqus 模型。但幸运的是,我们可以使用 Abaqus 提供的命令来访问它。即:
abaqus cae
[database=database-file]
[replay=replay-file]
[recover=journal-file]
[startup=startup-file]
[script=script-file]
[noGUI=noGUI-file]
[noenvstartup]
[noSavedOptions]
[noSavedGuiPrefs]
[noStartupDialog]
[custom=script-file]
[guiTester=GUI-script]
[guiRecord]
[guiNoRecord]
通常,我们可以使用 noGUI-file 或 script-file 选项在 Abaqu 中执行我们的 Python 脚本。
另一个 Python 解释器是我们自己安装的 Python 解释器,其中安装了 abqpy
。abqpy
提供了一个将我们的 Python 脚本连接到 Abaqus Python 解释器的桥梁,它为 Abaqus 的 Python 脚本提供了类型提示,使我们能够快速编写 Abaqus Python 脚本。
这个包是如何工作的?
abqpy
只是一个为 Abaqus/Python 脚本提供类型提示的包,它安装在 Abaqus/Python 环境之外,你可以使用 abqpy
来编写你的 Abaqus/Python 脚本,并自己在 Abaqus 里面运行脚本。但是,在 Abaqus 命令的帮助下,可以实现一种更简单的方法:** 您实际上可以使用自己的 Python 解释器运行脚本而无需打开 Abaqus**,这是通过 abaqus 命令实现的,如下所示:
abaqus cae noGUI=script.py
The secret is hided in the run()
function:
def run(cae = True):
abaqus = os.environ.get("ABAQUS_BAT_PATH", "abaqus")
filePath = os.path.abspath(__main__.__file__)
args = " ".join(sys.argv[1:])
if cae:
os.system(f"{abaqus} cae noGUI={filePath} -- {args}")
else:
os.system(f"{abaqus} python {filePath} {args}")
sys.exit(0)
在这个包中,abaqus
模块被重新实现以自动调用这个函数。 如果您在脚本导入此模块(如 from abaqus import *
),您的 Python 解释器(而非 Abaqus Python 解释器)将调用此函数并使用 abaqus 命令将脚本提交给 Abaqus。 提交给 Abaqus 后,run()
函数将退出解释器,因为脚本已经在 Abaqus Python 解释器中运行。
在输出脚本中,我们可能不想总是使用 abaqus
模块,因为它需要 Abaqus/CAE 内核(和它的许可证)。 这时,我们使用模块 odbAccess
(例如,from odbAccess import *
), 它只需要 Abaqus Python 解释器而不需要 Abaqus/CAE 内核。这时,我们需要另一个类似的 abaqus 命令:
abaqus python script.py
因此,odbAccess
模块也被重新实现以调用带有参数 cae = False
的 run()
函数。
总之,当您导入两个模块之一(abaqus
或 odbAccess
)时,将调用 run()
函数。 它将在 abaqus
模块中传入参数 cae = True
,在 odbAccess
模块中传入参数 cae = False
。 因此,如果您想在外部 Python 环境中运行您的 Python 脚本,请确保在您的脚本导入这些模块之一。
编写你的 Abaqus/Python 脚本
安装 abqpy
包后,您可以开始编写自己的 Abaqus/Python 脚本来构建模型。 您可以参考 abqpy/examples at main・hailiin/abqpy 获取一些脚本示例。 或者你可以去 教程 看一个简单的教程。 有关 Abaqus/Python 脚本的更多文档,请查看 Abaqus Class References 以获取更详细的 API 参考。
配置 Abaqus 环境
Make sure the abaqus
command is available in the command line (i.e., you can run abaqus
in the command line), otherwise,
add a new system variable named ABAQUS_BAT_PATH
, and set the value to the file path of the Abaqus command, for example,
C:/SIMULIA/Commands/abaqus.bat
.
运行你的 Abaqus/Python 脚本
Now you can run your Abaqus/Python script with the following methods:
Open Abaqus/CAE and click
Run Script
in the menu bar, then select your script file, which is the most common way to run a Python script in Abaqus/CAE.Use the
abaqus
command in the command line:abaqus cae script=script.py
See here for more information about the
abaqus
command.Use the
abqpy
command in the command line:[python -m] abqpy cae script.py
The advantage using
abqpy
command instead of usingabaqus
command directly is that you are able to customize the default python launch command. See 命令行接口 for more information about theabqpy
command.Use the Python 3 interpreter to run the script directly:
python script.py
This is the most convenient way to run the script, it is equivalent to the
abqpy
command with some default predefined arguments.Use the
abqpy.cli.abaqus
object (anabqpy.cli.AbqpyCLI
object) to run the script:from abqpy.cli import abaqus abaqus.cae(script="script.py")
The
abqpy.cli.abaqus
object is the object used for theabqpy
command, you can call the methods in this object directly to run the script. This method is convenient when you want to call the Abaqus/Python script in another Python script since typing annotations are provided for the methods, so you can check the docstring of the methods for more information.
警告
abqpy
不支持调试,因为 Abaqus 没有为 Abaqus/CAE 之外的 Python 脚本提供一个调试器。 如果您在调试模式下运行脚本,脚本将不会提交给 Abaqus。 但它将在安装了 abqpy
的外部 Python 解释器中运行 。 由于 abqpy
没有实现全部的 Abaqus/Python 接口,脚本可能无法(而且很可能)正常运行。 然而,对于一些简单的脚本,脚本可能可以正常工作,你可以使用调试器来粗略检查 Abaqus 中的变量。 但脚本仍然不会提交给 Abaqus。
评论