当前位置:   article > 正文

pyfluent使用记录(1):安装测试

pyfluent

pyfluent支持Python 3.7 Python 3.10Fluent版本最低为2022R2

1. 安装命令

利用下面的命令可以安装最新版的pyfluent:

pip install ansys-fluent-core

在CFD之道中还提到了“为方便使用,可以配套安装Fluent参数化模块及可视化、后处理模块。利用下面的命令安装”:

  1. pip install ansys-fluent-parametric
  2. pip install ansys-fluent-visualization

这两个pyfluent的库相关文档可以访问参考资料[3]查询详细信息。

2. 测试

可以直接在Python中输入下面的命令,也可以将其写入一个文档中再使用Python运行

  1. import ansys.fluent.core as pyfluent
  2. solver_session = pyfluent.launch_fluent(mode="solver")
  3. solver_session.health_check_service.is_serving

 测试对于Fluent 2023R1是没有问题的,但对于老版本的Fluent 2022R2会报错。

KeyError: 'AWP_ROOT231'

  1. Python 3.10.6 (tags/v3.10.6:9c7b4bd, Aug 1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)] on win32
  2. Type "help", "copyright", "credits" or "license" for more information.
  3. >>> import ansys.fluent.core as pyfluent
  4. >>> solver_session = pyfluent.launch_fluent(mode="solver")
  5. Traceback (most recent call last):
  6. File "<stdin>", line 1, in <module>
  7. File "C:\Users\Ding\AppData\Local\Programs\Python\Python310\lib\site-packages\ansys\fluent\core\launcher\launcher.py", line 579, in launch_fluent
  8. launch_string = _generate_launch_string(
  9. File "C:\Users\Ding\AppData\Local\Programs\Python\Python310\lib\site-packages\ansys\fluent\core\launcher\launcher.py", line 421, in _generate_launch_string
  10. exe_path = _get_fluent_exe_path(**argvals)
  11. 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
  12. return get_exe_path()
  13. File "C:\Users\Ding\AppData\Local\Programs\Python\Python310\lib\site-packages\ansys\fluent\core\launcher\launcher.py", line 128, in get_exe_path
  14. exe_path = get_fluent_path()
  15. File "C:\Users\Ding\AppData\Local\Programs\Python\Python310\lib\site-packages\ansys\fluent\core\launcher\launcher.py", line 122, in get_fluent_path
  16. path = os.environ["AWP_ROOT" + "".join(FLUENT_VERSION.split("."))[:-1]]
  17. File "C:\Users\Ding\AppData\Local\Programs\Python\Python310\lib\os.py", line 679, in __getitem__
  18. raise KeyError(key) from None
  19. 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中发现了一个设置版本的函数:

  1. def set_ansys_version(version: Union[str, float, FluentVersion]) -> None:
  2. """Set the Fluent version manually.
  3. This method only works if the provided Fluent version is installed
  4. and the environment variables are updated properly. This supersedes
  5. the Fluent path set in the environment variable.
  6. """
  7. global FLUENT_VERSION
  8. FLUENT_VERSION = str(FluentVersion(version))

那么就可以通过这个函数来设置fluent版本:

  1. import ansys.fluent.core as pyfluent
  2. pyfluent.set_ansys_version('22.2.0')
  3. solver_session = pyfluent.launch_fluent(mode="solver")
  4. solver_session.health_check_service.is_serving

 参考资料:

[1] CFD之道 pyFluent:利用python控制Fluent

[2] pyfluent github 仓库

[3]  pyANSYS

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/120799
推荐阅读
相关标签
  

闽ICP备14008679号