pyfluent支持Python 3.7 到 Python 3.10,Fluent版本最低为2022R2。
1. 安装命令
利用下面的命令可以安装最新版的pyfluent:
pip install ansys-fluent-core
在CFD之道中还提到了“为方便使用,可以配套安装Fluent参数化模块及可视化、后处理模块。利用下面的命令安装”:
pip install ansys-fluent-parametric
pip install ansys-fluent-visualization
这两个pyfluent的库相关文档可以访问参考资料[3]查询详细信息。
2. 测试
可以直接在Python中输入下面的命令,也可以将其写入一个文档中再使用Python运行:
import ansys.fluent.core as pyfluent
solver_session = pyfluent.launch_fluent(mode="solver")
solver_session.health_check_service.is_serving
测试对于Fluent 2023R1是没有问题的,但对于老版本的Fluent 2022R2会报错。
KeyError: 'AWP_ROOT231'
Python 3.10.6 (tags/v3.10.6:9c7b4bd, Aug 1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ansys.fluent.core as pyfluent
>>> solver_session = pyfluent.launch_fluent(mode="solver")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Ding\AppData\Local\Programs\Python\Python310\lib\site-packages\ansys\fluent\core\launcher\launcher.py", line 579, in launch_fluent
launch_string = _generate_launch_string(
File "C:\Users\Ding\AppData\Local\Programs\Python\Python310\lib\site-packages\ansys\fluent\core\launcher\launcher.py", line 421, in _generate_launch_string
exe_path = _get_fluent_exe_path(**argvals)
File "C:\Users\Ding\AppData\Local\Programs\Python\Python310\lib\site-packages\ansys\fluent\core\launcher\launcher.py", line 143, in _get_fluent_exe_path
return get_exe_path()
File "C:\Users\Ding\AppData\Local\Programs\Python\Python310\lib\site-packages\ansys\fluent\core\launcher\launcher.py", line 128, in get_exe_path
exe_path = get_fluent_path()
File "C:\Users\Ding\AppData\Local\Programs\Python\Python310\lib\site-packages\ansys\fluent\core\launcher\launcher.py", line 122, in get_fluent_path
path = os.environ["AWP_ROOT" + "".join(FLUENT_VERSION.split("."))[:-1]]
File "C:\Users\Ding\AppData\Local\Programs\Python\Python310\lib\os.py", line 679, in __getitem__
raise KeyError(key) from None
KeyError: 'AWP_ROOT231'
3. 错误解决
对于上述错误,我们通过官方的GitHub 帮助可以发现是环境变量设置的不对,但问题是我们安装好Fluent2022R2 的时候是自动设置了一个AWP_ROOT222的环境变量。
3.1 解决方案1
我们设置一个环境变量AWP_ROOT231,值同样指向AWP_ROOT222的位置:"C:\Program Files\ANSYS Inc\v222"。
此时就可以通过测试了。
3.2 解决方案2
"C:\Users\Ding\AppData\Local\Programs\Python\Python310\lib\site-packages\ansys\fluent\core\launcher\launcher.py"
我们直接打开pyfluent的launcher.py这个文件,可以发现是通过 FLUENT_VERSION这个参数来获取fluent版本的,从而去环境变量中找相关的路径。但在最新版pyfluent库中默认设置了:
FLUENT_VERSION = "23.1.0"
我们可以通过直接修改这个参数,从而改变默认的fluent版本:
FLUENT_VERSION ="22.2.0"
以及我们在launcher.py中发现了一个设置版本的函数:
def set_ansys_version(version: Union[str, float, FluentVersion]) -> None:
"""Set the Fluent version manually.
This method only works if the provided Fluent version is installed
and the environment variables are updated properly. This supersedes
the Fluent path set in the environment variable.
"""
global FLUENT_VERSION
FLUENT_VERSION = str(FluentVersion(version))
那么就可以通过这个函数来设置fluent版本:
import ansys.fluent.core as pyfluent
pyfluent.set_ansys_version('22.2.0')
solver_session = pyfluent.launch_fluent(mode="solver")
solver_session.health_check_service.is_serving