Thanks to visit codestin.com
Credit goes to github.com

Skip to content

sherlockyzd/pyOpenFOAM

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pyOpenFOAM

License Python NumPy JAX SciPy Matplotlib Stars

中文 | English


基于有限体积方法(FVM)的 Python 计算流体动力学求解器,兼容 OpenFOAM 网格格式,支持 NumPy / JAX 双后端架构。

📑 目录

✨ 特性

网格与边界

  • 结构化 / 非结构化网格
  • 多种边界条件(壁面、入口、出口、对称面等)
  • OpenFOAM 原生网格格式兼容

求解与算法

  • SIMPLE / PISO 压力-速度耦合
  • AMG、PCG、ILU、SOR 线性求解器
  • 稳态 / 瞬态模拟

后端架构

  • NumPy 默认后端(生产运行最快)
  • JAX 后端(自动微分 / GPU / JIT)
  • 一行配置切换

可视化

  • 模拟结束自动保存残差历史图
  • OpenFOAM 算例结构原生支持
  • 兼容 ParaView 后处理

🚀 快速开始

安装

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.py

代码调用

from 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 支持一下!

footer


English Version

English | 中文

A Python-based CFD solver implementing the Finite Volume Method (FVM), compatible with OpenFOAM mesh formats, supporting NumPy / JAX dual backends.

✨ Key Features

  • 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

🚀 Quick Start

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   # Lid-driven cavity
cd example/elbow  && python pyFVMScript.py   # Pipe elbow
cd example/flange && python pyFVMScript.py   # Heat conduction

⚡ Backend Switch

# src/config.py
cfdBackend = 'numpy'   # Default
# cfdBackend = 'jax'   # pip install jax jaxlib

🧪 Example Cases

Case Type Mesh Residual
cavity Incompressible Structured
elbow Incompressible Unstructured
flange Heat transfer Unstructured

📁 Structure

pyOpenFOAM/
├── src/cfdtool/     # Backend, solvers, plotting
├── src/pyFVM/       # Core FVM: Region, Assemble, Polymesh
├── example/         # cavity, elbow, flange
├── LICENSE
└── README.md

📚 References

  • Moukalled et al., The Finite Volume Method in CFD. Springer.
  • OpenFOAM

If you find this useful, give it a ⭐ Star!

footer

About

pyOpenFOAM is a Python-based Computational Fluid Dynamics (CFD) library implementing the Finite Volume Method (FVM), designed for simulating fluid flow and heat transfer phenomena based on the principles from the book 《The Finite Volume Method in Computational Fluid Dynamics 》.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors