中文 | English
基于有限体积方法(FVM)的 Python 计算流体动力学求解器,兼容 OpenFOAM 网格格式,支持 NumPy / JAX 双后端架构。
|
网格与边界
|
求解与算法
|
|
后端架构
|
可视化
|
git clone https://github.com/sherlockyzd/pyOpenFOAM.git
cd pyOpenFOAM
conda create --name pyOF python=3.10 numpy matplotlib scipy
conda activate pyOF# 顶盖驱动方腔流(结构化网格)
cd example/cavity && python pyFVMScript.py
# 90° 弯管流动(非结构化网格)
cd example/elbow && python pyFVMScript.py
# 法兰盘热传导(非结构化网格)
cd example/flange && python pyFVMScript.pyfrom pyFVM.Region import Region
case = Region("/path/to/case")
case.RunCase()
# 模拟结束后,算例目录下自动生成 residualHistory.png| 方程 | 说明 |
|---|---|
| 动量方程 | Navier-Stokes |
| 连续性方程 | 质量守恒 |
| 能量方程 | 传热 |
| 压力-速度耦合 | SIMPLE / PISO |
| 算例 | 类型 | 网格 | 残差收敛 |
|---|---|---|---|
cavity |
不可压缩流 | 结构化 | ![]() |
elbow |
不可压缩流 | 非结构化 | ![]() |
flange |
传热 | 非结构化 | ![]() |
修改 src/config.py:
cfdBackend = 'numpy' # 默认,生产运行最快
# cfdBackend = 'jax' # JAX 后端(需 pip install jax jaxlib)| 后端 | 性能 | 适用场景 |
|---|---|---|
| NumPy | cavity ~6s | 日常开发、生产计算 |
| JAX | 慢 2-3x(无 JIT) | 自动微分、GPU 并行、JIT 编译 |
JAX 后端为以下能力铺路:
jax.jit— 编译计算图 → GPU 加速jax.grad— 自动微分 → 反演优化、数据同化jax.vmap/jax.pmap— 向量化与多设备并行
pyOpenFOAM/
├── src/
│ ├── cfdtool/ # 工具模块
│ │ ├── backend.py # 后端抽象 + NumpyBackend(24 API)
│ │ ├── backend_jax.py # JaxBackend 实现
│ │ ├── config.py # 后端选择
│ │ ├── Solve.py # 线性求解器
│ │ └── cfdPlot.py # 残差绘图
│ └── pyFVM/ # 核心 FVM
│ ├── Region.py # 仿真控制器
│ ├── Assemble.py # 方程组装
│ ├── Polymesh.py # 网格处理
│ └── ...
├── example/
│ ├── cavity/ # 方腔流(结构化)
│ ├── elbow/ # 弯管流(非结构化)
│ └── flange/ # 传热(非结构化)
├── LICENSE
└── README.md
- Moukalled, F., Mangani, L., & Darwish, M. The Finite Volume Method in Computational Fluid Dynamics: An Advanced Introduction with OpenFOAM and Matlab. Springer.
- OpenFOAM — 开源 CFD 工具箱
如果觉得有用,欢迎 ⭐ Star 支持一下!
English | 中文
A Python-based CFD solver implementing the Finite Volume Method (FVM), compatible with OpenFOAM mesh formats, supporting NumPy / JAX dual backends.
- Multi-dimensional mesh — Structured and unstructured with various boundary conditions
- Multiple solvers — AMG, PCG, ILU, SOR
- SIMPLE / PISO — Steady-state and transient simulations
- Dual-backend — NumPy (default) and JAX, switch with one line
- OpenFOAM compatible — Native mesh format and case structure support
- Auto visualization — Residual history plot saved automatically
git clone https://github.com/sherlockyzd/pyOpenFOAM.git
cd pyOpenFOAM
conda create --name pyOF python=3.10 numpy matplotlib scipy
conda activate pyOFcd example/cavity && python pyFVMScript.py # Lid-driven cavity
cd example/elbow && python pyFVMScript.py # Pipe elbow
cd example/flange && python pyFVMScript.py # Heat conduction# src/config.py
cfdBackend = 'numpy' # Default
# cfdBackend = 'jax' # pip install jax jaxlib| Case | Type | Mesh | Residual |
|---|---|---|---|
cavity |
Incompressible | Structured | ![]() |
elbow |
Incompressible | Unstructured | ![]() |
flange |
Heat transfer | Unstructured | ![]() |
pyOpenFOAM/
├── src/cfdtool/ # Backend, solvers, plotting
├── src/pyFVM/ # Core FVM: Region, Assemble, Polymesh
├── example/ # cavity, elbow, flange
├── LICENSE
└── README.md
- Moukalled et al., The Finite Volume Method in CFD. Springer.
- OpenFOAM
If you find this useful, give it a ⭐ Star!


