使用说明
用于铸造和冶金模拟的OpenFOAM MCP服务器
MCP(模型上下文协议)服务器,使Claude等AI代理能够与OpenFOAM交互,用于铸造和冶金模拟。
🎯 特性
- 12+铸造专用工具 对于AI代理
- 自动案例设置 适用于不同的铸件类型
- 材料数据库 具有普通金属和模具材料的特性
- 缺陷预测 (孔隙、收缩、热点、冷隔、错流)
- 参数优化 用于门控系统
- 多物理支持 (模具填充、固化、传热)
- 并行执行 支持更快的模拟
📋 先决条件
必需
- Python 3.10+
- OpenFOAM v11或更高版本 (v2306、v2312等)
- 克劳德代码 或其他MCP兼容客户端
可选的
- ParaView 用于可视化
- PyFoam 用于高级OpenFOAM文件解析
- 多核cpu 用于并行模拟
🚀 安装
1.安装OpenFOAM
Ubuntu/Debian
# Add OpenFOAM repository
sudo sh -c "wget -O - http://dl.openfoam.org/gpg.key | apt-key add -"
sudo add-apt-repository http://dl.openfoam.org/ubuntu
# Install OpenFOAM
sudo apt-get update
sudo apt-get install openfoam11
# Source OpenFOAM environment
source /opt/openfoam11/etc/bashrc
# Add to your ~/.bashrc for persistence
echo "source /opt/openfoam11/etc/bashrc" >> ~/.bashrc
Docker(替代方案)
docker pull openfoam/openfoam11-paraview512
2.安装OpenFOAM MCP服务器
# Clone repository
git clone https://github.com/yourusername/openfoam-mcp.git
cd openfoam-mcp
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\\Scripts\\activate
# Install package
pip install -e .
# Or install dependencies directly
pip install -r requirements.txt
3.配置克劳德代码
添加到您的 ~/.claude/mcp.json:
{
"mcpServers": {
"openfoam": {
"command": "python",
"args": ["-m", "openfoam_mcp.server"],
"env": {
"FOAM_INST_DIR": "/opt/openfoam11"
}
}
}
}
🎮 用法
快速入门示例
# Using Claude Code CLI
claude-code
# Then ask Claude:
> "Create a mold filling simulation for an aluminum casting at 750°C with a sand mold"
# Claude will:
# 1. Create the case
# 2. Set up geometry
# 3. Configure material properties
# 4. Generate mesh
# 5. Run simulation
# 6. Analyze results for defects
可用工具
| 工具 | 说明 |
|---|---|
create_casting_case |
创建新的OpenFOAM案例 |
list_cases |
列出所有案例 |
setup_geometry |
配置几何图形(STL或参数化) |
setup_material_properties |
设置金属和模具属性 |
setup_boundary_conditions |
配置BC |
run_mesh_generation |
生成计算网格 |
run_simulation |
执行OpenFOAM求解器 |
analyze_results |
分析仿真结果 |
predict_defects |
预测铸件缺陷 |
export_results |
导出结果(VTK、STL、CSV) |
get_case_status |
检查案例状态 |
optimize_gating_system |
优化闸门/立管位置 |
支持的铸造类型
- 充模:重力浇注,低压,高压
- 凝固:具有传热和相变
- 连铸:钢、铝连铸
- 压铸:高压压铸
支持的材料
金属:
- 钢(碳钢)
- 铝(纯铝和合金铝)
- 铁(铸铁)
- 铜
- 青铜
模具材料:
- 沙
- 陶瓷
- 金属(永久模具)
- 石墨
📊 工作流示例
1.创建案例
# Claude command:
"Create a solidification simulation for steel at 1650°C in a sand mold named steel_casting_01"
2.添加几何图形
# Option A: From STL file
"Set up geometry for steel_casting_01 using the STL file at /path/to/casting.stl with fine mesh"
# Option B: Parametric
"Set up a simple box geometry for steel_casting_01: 0.2m x 0.15m x 0.1m with medium mesh"
3.运行模拟
"Run the simulation for steel_casting_01 using interPhaseChangeFoam for 100 seconds"
4.分析结果
"Analyze steel_casting_01 and predict all defects"
5.优化
"Optimize the gating system for steel_casting_01 to minimize porosity"
🏗️ 建筑
┌─────────────────┐ MCP Protocol ┌──────────────────┐
│ Claude AI │ ←──────────────────────→ │ OpenFOAM MCP │
│ │ │ Server │
└─────────────────┘ └──────────────────┘
↓
┌──────────────────┐
│ API Managers │
│ - OpenFOAMClient│
│ - CaseManager │
│ - ResultAnalyzer│
└──────────────────┘
↓
┌──────────────────┐
│ Case Builders │
│ - Templates │
│ - Material DB │
└──────────────────┘
↓
┌──────────────────┐
│ OpenFOAM Core │
│ (Solvers) │
└──────────────────┘
🧪 测试
# Run all tests
pytest
# Run with coverage
pytest --cov=openfoam_mcp --cov-report=html
# Run specific test
pytest tests/test_case_manager.py
📁 项目结构
openfoam-mcp/
├── openfoam_mcp/
│ ├── __init__.py
│ ├── server.py # Main MCP server
│ ├── api/
│ │ ├── __init__.py
│ │ ├── openfoam_client.py # OpenFOAM command execution
│ │ ├── case_manager.py # Case creation/management
│ │ └── result_analyzer.py # Result analysis
│ ├── builders/
│ │ ├── __init__.py
│ │ ├── case_builder.py # Case file builder
│ │ └── templates.py # OpenFOAM file templates
│ └── utils/
│ └── __init__.py
├── tests/
│ ├── test_server.py
│ ├── test_case_manager.py
│ └── test_openfoam_client.py
├── examples/
│ ├── simple_mold_filling.md
│ └── solidification_analysis.md
├── docs/
│ ├── getting_started.md
│ └── tool_reference.md
├── pyproject.toml
├── requirements.txt
└── README.md
🔧 配置
环境变量
FOAM_INST_DIR:OpenFOAM安装目录(默认:/opt/openfoam11)OPENFOAM_RUN_DIR:模拟案例目录(默认:~/foam/run)
自定义材料数据库
您可以通过编辑来扩展材质数据库 openfoam_mcp/builders/case_builder.py:
self.metal_database["my_alloy"] = {
"density": 8000,
"viscosity": 0.005,
"thermal_conductivity": 40,
"specific_heat": 500,
"liquidus_temp": 1700,
"solidus_temp": 1500,
"latent_heat": 250000
}
🤝 贡献
欢迎投稿!拜托:
- 克隆该仓库
- 创建要素分支(
git checkout -b feature/amazing-feature) - 提交您的更改(
git commit -m 'Add amazing feature') - 推到分支(
git push origin feature/amazing-feature) - 打开拉取请求
📝 路线图
- []增加更多铸件类型(熔模铸造、离心铸造)
- []实施实时进度监控
- []添加基于机器学习的缺陷预测
- []支持更多OpenFOAM求解器
- []与商业铸造软件集成以进行验证
- []基于Web的可视化界面
- []自动浇注系统设计
- []多目标优化
🐛 已知问题
- 复杂几何形状的网格生成可能需要手动调整snappyHexMesh
- 大型模拟(>1M单元)需要并行执行
- 温度相关材料特性尚未实现
📚 参考文献
OpenFOAM资源
铸造模拟
MCP协议
📄 许可证
此项目根据MIT许可证获得许可-有关详细信息,请参阅许可证文件。
🙏 致谢
- OpenFOAM基金会,卓越的CFD工具包
- 模型上下文协议的拟人化
- 铸造和冶金研究界
📧 联系
如有疑问或支持:
- 在GitHub上打开一个问题
- 电子邮件:your.email@example.com
内置于❤️ 面向铸造和冶金工程界