具有真实AFM数据集成的FerroSim MCP服务器
将FerroSim铁电模拟与真实AFM实验数据分析相结合的模型上下文协议(MCP)服务器。
参考文献
本项目建立在以下基础之上:
- DTMicroscope数字孪生框架:
- AFM黑客马拉松Colab笔记本: https://colab.research.google.com/drive/1yR698-\_qoAKoiK6VRVbU5UFJUkV5tS_l
特性
FerroSim理论侧
- 创建并运行铁电畴模拟
- 配置电场、缺陷和材料属性
- 可视化极化动力学
- 导出模拟结果
AFM实验侧
- 加载真实AFM数据 多种格式(.ibw、.h5、.npy、.mat、.txt)
- 分析铁电畴结构
- 提取域面积、墙密度和统计信息
- DTMicrosoft-compatible API
- 逐行扫描仿真器
理论实验匹配
- 将模拟结果与AFM实验数据进行比较
- 计算相关性、MSE、RMSE指标
- 评估比赛质量
快速开始
安装
# Activate conda environment
source /Users/guanlinhe/miniconda3/bin/activate ferrosim_mcp
# Install dependencies
pip install mcp ferrosim git+https://github.com/ramav87/FerroSim.git@rama-dev
pip install igor2 scipy h5py numpy用法
1.加载真实AFM数据
from afm_digital_twin import AFMDigitalTwin
# Initialize
afm = AFMDigitalTwin()
# Load data (format auto-detected from extension)
result = afm.load_data("AFM/temp/scan_0001.ibw") # Igor format
# OR
result = afm.load_data("data/dset_spm1.h5") # HDF5/USID format
print(f"Loaded scan: {result['scan_id']}")
print(f"Format: {result['format']}")2.分析域名
# Analyze domain structure
domains = afm.analyze_domain_structure()
print(f"Up domain area: {domains['up_domain_area']*1e12:.2f} μm²")
print(f"Down domain area: {domains['down_domain_area']*1e12:.2f} μm²")
print(f"Wall density: {domains['domain_wall_density']:.3f}")3.运行MCP服务器
python ferrosim_mcp_server_minimal.py支持的文件格式
| 格式 | 扩展名 | 描述 | 状态 |
|---|---|---|---|
| Igor二元波 | .ibw | Igor Pro的PFM数据 | ✅ 已测试 |
| HDF5/USID | .h5, .hdf5 | DTM显微镜/sidpy格式 | ✅ 已测试 |
| NumPy | .npy | 二进制NumPy数组 | ✅ 工作 |
| MATLAB | .mat | MATLAB数据文件 | ✅ 工作 |
| 文本 | .txt, .dat | 纯文本数组 | ✅ 工作 |
多渠道支持: 自动检测PFM数据中的振幅和相位通道。
api参考
AFM数字孪生
load_data(filepath, data_format=None)
从文件加载真实的AFM扫描数据。
参数:
filepath(str):数据文件的路径data_format(str,可选):格式('ibw','h5','npy','mat','tx')。如果无,则自动检测。
退货: 包含scan_id和元数据的词典
analyze_domain_structure(scan_id=None)
分析铁电畴结构。
退货: 词典包含:
num_up_domains:像素数num_down_domains:像素数up_domain_area:面积(平方米)down_domain_area:面积(平方米)domain_wall_density:壁密度度量mean_amplitude_up/down:按域划分的平均振幅
get_piezoresponse(scan_id=None)
获取PFM振幅和相位数据。
退货: 带振幅和相位阵列的词典
get_scan(scan_id=None, channels=None)
获取扫描数据(与DTMicrosoft-compatible API兼容)。
退货: (array_list、shape、dtype)元组
scanning_emulator(scan_id=None, scanning_rate=10)
模拟逐行扫描。
产量: 每条扫描线的行数据
MCP工具
AFM工具
afm_load_real_scan:加载真实的AFM数据文件afm_analyze_domains:分析域结构afm_get_piezoresponse:获取振幅/相位数据afm_list_scans:列出所有加载的扫描afm_get_scan:获取扫描数据(DTMicrosoftcope API)afm_go_to:将探头移动到位置afm_get_position:获取当前探头位置
FerroSim工具
initialize_simulation:创建新的模拟run_simulation:执行模拟get_simulation_results:检索结果visualize_simulation:生成绘图list_simulations:列出所有模拟
理论实验匹配
match_simulation_to_afm:将模拟与AFM数据进行比较
工作流示例
完整的分析流程
from afm_digital_twin import AFMDigitalTwin
import numpy as np
# 1. Initialize and load data
afm = AFMDigitalTwin()
result = afm.load_data("AFM/temp/scan_0001.ibw")
scan_id = result['scan_id']
# 2. Analyze domains
domains = afm.analyze_domain_structure(scan_id)
print(f"Domain Analysis Results:")
print(f" Up domain area: {domains['up_domain_area']*1e12:.3f} μm²")
print(f" Down domain area: {domains['down_domain_area']*1e12:.3f} μm²")
print(f" Wall density: {domains['domain_wall_density']:.3f}")
# 3. Get piezoresponse
pfm = afm.get_piezoresponse(scan_id)
amplitude = np.array(pfm['amplitude'])
phase = np.array(pfm['phase'])
# 4. Use DTMicroscope API
array_list, shape, dtype = afm.get_scan(scan_id, channels=['Amplitude', 'Phase'])
data = np.array(array_list, dtype=dtype).reshape(shape)
# 5. Emulate scanning
for line_idx, line_data in enumerate(afm.scanning_emulator(scan_id)):
# Process line-by-line
amplitude_line, phase_line = line_data
# ... process data
if line_idx >= 10:
break测试脚本
test_real_afm_workflow.py
全面的工作流程测试演示:
- 数据加载
- 领域分析
- 压电响应提取
- DTMicrosoftcope API兼容性
- 扫描仿真器
运行:
source /Users/guanlinhe/miniconda3/bin/activate ferrosim_mcp
python test_real_afm_workflow.py数据目录结构
AFM/
├── temp/
│ └── scan_0001.ibw # Real experimental PFM data
├── 0_AFM_Basic_functionality_COLAB_Hackaton.ipynb
├── 1_AFM_Imperfect_Probe_COLAB_Hackaton.ipynb
└── ...建筑
简化设计
该系统已经过简化,专注于真实数据:
- 无合成数据生成:删除所有合成/模拟数据生成
- 仅限真实数据:AFM Digital Twin仅加载和提供实验数据
- DTM显微镜兼容性:遵循DTMicrosoftcope API模式
- 清洁API:具有基本功能的简化工具集
核心组件
ferrosim_mcp_server_minimal.py # MCP server with FerroSim + AFM tools
afm_digital_twin.py # Real AFM data loader and analyzer配置
Claude桌面集成
添加到 ~/Library/Application Support/Claude/claude_desktop_config.json:
{
"mcpServers": {
"ferrosim-afm": {
"command": "/Users/guanlinhe/miniconda3/envs/ferrosim_mcp/bin/python",
"args": [
"/Users/guanlinhe/github/hackMCP/ferrosim_mcp_server_minimal.py"
],
"env": {
"PYTHONPATH": "/Users/guanlinhe/github/hackMCP"
}
}
}
}需求
mcp>=0.1.0
numpy>=1.20.0
scipy>=1.7.0
igor2>=0.5.0
h5py>=3.0.0
ferrosim许可证
仅供研究使用。有关详细信息,请参阅单个组件许可证。
引用
如果您使用此代码,请引用:
- FerroSim:
- DTM显微镜:
