> For the complete documentation index, see [llms.txt](https://docs.clore.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.clore.ai/guides/guides_v2-zh/ke-xue-yu-yan-jiu/gromacs.md).

# GROMACS 分子动力学

> **GPU加速分子动力学模拟——从蛋白质折叠到药物发现**

GROMACS（GROningen MAchine for Chemical Simulations）是全球使用最广泛的分子动力学模拟软件包。它最初由格罗宁根大学开发，如今由全球社区维护，是世界各地计算化学和结构生物学实验室的主力工具。

借助 GPU 加速，GROMACS 可以以仅靠 CPU 硬件需要数周才能完成的速度模拟数百万原子的系统。Clore.ai 价格实惠的 GPU 租用服务让大型 MD 模拟也能被个人研究者和小型实验室轻松使用。

***

## 你可以模拟什么？

* **蛋白质折叠与动力学** — 观察纳秒到微秒尺度的构象变化
* **药物-蛋白质结合** — 计算用于药物发现的结合自由能
* **膜模拟** — 脂双层、膜蛋白、离子传输
* **蛋白质-蛋白质相互作用** — 研究复合物形成和界面动力学
* **材料科学** — 聚合物、纳米颗粒、水模型
* **自由能计算** — 代谢变换，PME

***

## 前提条件

* 带 GPU 租赁的 Clore.ai 账户
* 基本的 Linux 命令行知识
* 分子系统文件（拓扑 + 坐标），或使用示例系统
* 可选：本地安装 GROMACS 用于可视化（VMD、Pymol）

***

## 为什么使用 GPU 加速的 GROMACS？

启用 GPU 卸载的 GROMACS 可带来显著加速：

| 系统规模    | 仅 CPU（ns/天） | 单个 A100（ns/天） | 加速倍数   |
| ------- | ----------- | ------------- | ------ |
| 25K 原子  | \~50        | \~800         | \~16 倍 |
| 100K 原子 | \~15        | \~400         | \~27 倍 |
| 500K 原子 | \~3         | \~150         | \~50 倍 |
| 100 万原子 | \~1         | \~80          | \~80 倍 |

{% hint style="success" %}
**GPU 加速对大型系统（>100K 原子）最有帮助。** 对于小型测试系统，由于数据传输开销，CPU 性能可能相当。
{% endhint %}

***

## 步骤 1——在 Clore.ai 上租用 GPU

1. 前往 [clore.ai](https://clore.ai) → **市场**
2. 按 GPU 过滤： **A100、RTX 4090 或 RTX 3090** 推荐
3. 对于大型系统（>500K 原子）：选择 A100 40GB 或 80GB
4. 对于标准模拟：RTX 4090 或 RTX 3090 性价比极高

**推荐配置：**

* GPU：A100 40GB 或 RTX 4090
* CPU：16 核以上（GROMACS 会对非键相互作用使用多核）
* 内存：32GB+
* 磁盘：50GB+（轨迹文件可能很大）

***

## 第 2 步——部署 GROMACS 容器

使用 NVIDIA 官方 HPC GROMACS 镜像——它针对支持 CUDA 的 NVIDIA GPU 进行了优化：

**Docker 镜像：**

```
nvcr.io/hpc/gromacs:2023.2
```

**暴露端口：**

```
22
```

**环境变量：**

```
NVIDIA_VISIBLE_DEVICES=all
NVIDIA_DRIVER_CAPABILITIES=compute,utility
GMX_GPU_DD_COMMS=true
GMX_GPU_PME_PP_COMMS=true
GMX_FORCE_UPDATE_DEFAULT_GPU=true
```

{% hint style="info" %}
**GROMACS 的 NVIDIA 环境变量：**

* `GMX_GPU_DD_COMMS=true` — 启用基于 GPU 的域分解通信
* `GMX_GPU_PME_PP_COMMS=true` — 启用基于 GPU 的 PME-PP 通信
* `GMX_FORCE_UPDATE_DEFAULT_GPU=true` — 强制使用 GPU 坐标更新（显著提速）
  {% endhint %}

***

## 第 3 步——连接并验证

```bash
ssh root@<server-ip> -p <ssh-port>

# 检查 GROMACS 版本
gmx --version

# 检查 GPU 是否可用
nvidia-smi

# 验证 GROMACS 能否识别 GPU
gmx mdrun -h 2>&1 | grep -i gpu
```

来自 `gmx --version` 的预期输出应显示：

```
GROMACS 版本：2023.2
CUDA 版本：11.x 或 12.x
GPU 支持：CUDA
```

***

## 第 4 步——准备你的系统

### 使用示例系统（溶剂中的溶菌酶）

这是经典的 GROMACS 教程系统——非常适合测试你的环境：

```bash
# 创建工作目录
mkdir -p /workspace/lysozyme && cd /workspace/lysozyme

# 下载溶菌酶 PDB 结构
wget https://files.rcsb.org/download/1AKI.pdb -O 1AKI.pdb

# 从晶体结构中去除水分子
grep -v HOH 1AKI.pdb > 1AKI_clean.pdb

# 使用 AMBER99SB 力场生成拓扑
gmx pdb2gmx \
    -f 1AKI_clean.pdb \
    -o processed.gro \
    -water spce \
    -ff amber99sb-ildn
```

当提示选择力场时，选择 `amber99sb-ildn` （通常是选项 6）。

***

## 第 5 步——构建模拟盒

```bash
# 定义模拟盒（十二面体，距蛋白 1.0 nm）
gmx editconf \
    -f processed.gro \
    -o boxed.gro \
    -c \
    -d 1.0 \
    -bt dodecahedron

# 用水溶剂化模拟盒
gmx solvate \
    -cp boxed.gro \
    -cs spc216.gro \
    -o solvated.gro \
    -p topol.top

# 添加离子以中和电荷
# 首先为添加离子创建 tpr 文件
gmx grompp \
    -f /usr/local/gromacs/share/gromacs/top/em.mdp \
    -c solvated.gro \
    -p topol.top \
    -o ions.tpr

# 添加 Na+ 和 Cl- 离子（0.15M NaCl）
gmx genion \
    -s ions.tpr \
    -o ionized.gro \
    -p topol.top \
    -pname NA \
    -nname CL \
    -neutral \
    -conc 0.15
# 提示时选择第 13 组（SOL）
```

***

## 第 6 步——能量最小化

```bash
# 创建能量最小化 MDP 文件
cat > em.mdp << 'EOF'
; 能量最小化参数
integrator      = steep         ; 最陡下降法最小化
emtol           = 1000.0        ; 当最大力 < 1000 kJ/mol/nm 时停止
emstep          = 0.01          ; 初始步长
nsteps          = 50000         ; 最大最小化步数
nstlist         = 1
cutoff-scheme   = Verlet
ns_type         = grid
coulombtype     = PME
rcoulomb        = 1.0
rvdw            = 1.0
pbc             = xyz
EOF

# 为能量最小化准备 TPR
gmx grompp \
    -f em.mdp \
    -c ionized.gro \
    -p topol.top \
    -o em.tpr

# 在 GPU 上运行能量最小化
gmx mdrun \
    -v \
    -deffnm em \
    -gpu_id 0 \
    -ntmpi 1 \
    -ntomp 8

# 检查能量是否收敛
gmx energy -f em.edr -o em_potential.xvg
# 选择 10（势能），然后输入 0 退出
```

***

## 第 7 步——NVT 平衡（温度）

```bash
cat > nvt.mdp << 'EOF'
; NVT 平衡
define              = -DPOSRES      ; 位置约束
integrator          = md            ; Leap-frog 积分器
nsteps              = 50000         ; 100 ps（2 fs 步长）
dt                  = 0.002         ; 2 fs 步长
nstxout             = 500
nstvout             = 500
nstenergy           = 500
nstlog              = 500
continuation        = no
constraint_algorithm = lincs
constraints         = h-bonds
lincs_iter          = 1
lincs_order         = 4
cutoff-scheme       = Verlet
ns_type             = grid
nstlist             = 10
rcoulomb            = 1.0
rvdw                = 1.0
DispCorr            = EnerPres
coulombtype         = PME
pme_order           = 4
fourierspacing      = 0.16
tcoupl              = V-rescale
tc-grps             = Protein Non-Protein
tau_t               = 0.1 0.1
ref_t               = 300 300
pcoupl              = no
pbc                 = xyz
EOF

gmx grompp \
    -f nvt.mdp \
    -c em.gro \
    -r em.gro \
    -p topol.top \
    -o nvt.tpr

gmx mdrun \
    -deffnm nvt \
    -gpu_id 0 \
    -ntmpi 1 \
    -ntomp 8 \
    -nb gpu \
    -bonded gpu \
    -pme gpu \
    -update gpu
```

***

## 第 8 步——NPT 平衡（压力）

```bash
cat > npt.mdp << 'EOF'
; NPT 平衡
define              = -DPOSRES
integrator          = md
nsteps              = 50000
dt                  = 0.002
nstxout             = 500
nstvout             = 500
nstenergy           = 500
nstlog              = 500
continuation        = yes
constraint_algorithm = lincs
constraints         = h-bonds
cutoff-scheme       = Verlet
ns_type             = grid
nstlist             = 10
rcoulomb            = 1.0
rvdw                = 1.0
DispCorr            = EnerPres
coulombtype         = PME
tcoupl              = V-rescale
tc-grps             = Protein Non-Protein
tau_t               = 0.1 0.1
ref_t               = 300 300
pcoupl              = Parrinello-Rahman
pcoupltype          = isotropic
tau_p               = 2.0
ref_p               = 1.0
compressibility     = 4.5e-5
refcoord_scaling    = com
pbc                 = xyz
EOF

gmx grompp \
    -f npt.mdp \
    -c nvt.gro \
    -r nvt.gro \
    -t nvt.cpt \
    -p topol.top \
    -o npt.tpr

gmx mdrun \
    -deffnm npt \
    -gpu_id 0 \
    -ntmpi 1 \
    -ntomp 8 \
    -nb gpu \
    -bonded gpu \
    -pme gpu \
    -update gpu
```

***

## 第 9 步——生产性 MD 运行

```bash
cat > md.mdp << 'EOF'
; 生产性 MD 运行
integrator          = md
nsteps              = 5000000      ; 10 ns（2 fs 步长）
dt                  = 0.002
nstxout-compressed  = 5000        ; 每 10 ps 保存一次坐标
nstenergy           = 5000
nstlog              = 5000
continuation        = yes
constraint_algorithm = lincs
constraints         = h-bonds
cutoff-scheme       = Verlet
ns_type             = grid
nstlist             = 10
rcoulomb            = 1.0
rvdw                = 1.0
DispCorr            = EnerPres
coulombtype         = PME
tcoupl              = V-rescale
tc-grps             = Protein Non-Protein
tau_t               = 0.1 0.1
ref_t               = 300 300
pcoupl              = Parrinello-Rahman
pcoupltype          = isotropic
tau_p               = 2.0
ref_p               = 1.0
compressibility     = 4.5e-5
pbc                 = xyz
EOF

gmx grompp \
    -f md.mdp \
    -c npt.gro \
    -t npt.cpt \
    -p topol.top \
    -o md.tpr

# 完整 GPU 卸载生产运行
gmx mdrun \
    -deffnm md \
    -gpu_id 0 \
    -ntmpi 1 \
    -ntomp 16 \
    -nb gpu \
    -bonded gpu \
    -pme gpu \
    -update gpu \
    -v
```

{% hint style="info" %}
**实时监控进度：**

```bash
tail -f md.log | grep -E "(ns/day|Step|Time)"
```

{% endhint %}

***

## 第 10 步——分析

### 基础轨迹分析

```bash
# RMSD（随时间变化的主链稳定性）
gmx rms \
    -s md.tpr \
    -f md.xtc \
    -o rmsd.xvg \
    -tu ns
# 选择 4（Backbone）作为参考组和拟合组

# RMSF（每个残基的柔性）
gmx rmsf \
    -s md.tpr \
    -f md.xtc \
    -o rmsf.xvg \
    -res
# 选择 4（Backbone）

# 回转半径（紧凑性）
gmx gyrate \
    -s md.tpr \
    -f md.xtc \
    -o gyrate.xvg
# 选择 1（Protein）

# 氢键
gmx hbond \
    -s md.tpr \
    -f md.xtc \
    -num hbonds.xvg
# 供体和受体都选择 1（Protein）
```

### 绘制 XVG 文件

```python
import numpy as np
import matplotlib.pyplot as plt

# 加载 RMSD 数据
data = np.loadtxt('rmsd.xvg', comments=['@', '#'])
time = data[:, 0]      # 单位为 ns
rmsd = data[:, 1] * 10 # 将 nm 转换为 Å

plt.figure(figsize=(10, 4))
plt.plot(time, rmsd)
plt.xlabel('时间 (ns)')
plt.ylabel('RMSD (Å)')
plt.title('主链 RMSD')
plt.grid(True, alpha=0.3)
plt.savefig('rmsd_plot.png', dpi=150, bbox_inches='tight')
```

### 传输结果

```bash
# 从你的本地机器：
rsync -avz -e "ssh -p <ssh-port>" \
    root@<server-ip>:/workspace/lysozyme/ \
    ./md_results/
```

***

## 多 GPU 模拟

对于非常大的系统，使用带域分解的多个 GPU：

```bash
# 4 GPU 运行（调整 -ntmpi 与 GPU 数量一致）
gmx mdrun \
    -deffnm md \
    -gpu_id 0123 \
    -ntmpi 4 \
    -ntomp 4 \
    -nb gpu \
    -bonded gpu \
    -pme gpu \
    -npme 1 \
    -update gpu \
    -v
```

{% hint style="warning" %}
**多 GPU 效率：** 扩展到 4 个以上 GPU 通常只对原子数超过 100 万的系统有益。对于更小的系统，在 Clore.ai 上使用单个高端 GPU 更具成本效益。
{% endhint %}

***

## 故障排查

### 致命错误：无 GPU 卸载

```bash
# 检查 CUDA 是否正常工作
nvidia-smi
python3 -c "import ctypes; ctypes.CDLL('libcuda.so')"

# 强制回退到 CPU 以便测试
gmx mdrun -deffnm md -ntmpi 1 -ntomp 16
```

### 系统爆炸 / 负体积

这通常表明能量最小化存在问题：

```bash
# 使用更小步长运行更长时间的最小化
# 编辑 em.mdp：emstep = 0.001, emtol = 100
```

### 性能缓慢

```bash
# 在运行期间检查 GPU 利用率
watch -n 1 nvidia-smi

# 调整 GPU 卸载设置
gmx mdrun -deffnm md -nb gpu -pme gpu -bonded gpu -update gpu \
    -ntmpi 1 -ntomp $(nproc)
```

***

## 常见力场

| 力场               | 最适合       |
| ---------------- | --------- |
| `amber99sb-ildn` | 蛋白质，一般用途  |
| `charmm36m`      | 蛋白质 + 脂质膜 |
| `gromos54a7`     | 类药物分子     |
| `oplsaa`         | 有机分子、脂类   |

***

## 成本估算

| 模拟         | 系统规模   | GPU      | 时间     | 成本      |
| ---------- | ------ | -------- | ------ | ------- |
| 10 ns 蛋白质  | 25K 原子 | RTX 3090 | \~2 小时 | \~$0.60 |
| 100 ns 蛋白质 | 25K 原子 | A100 40G | \~6小时  | \~$4.50 |
| 100 ns 膜   | 20万个原子 | A100 80G | \~12小时 | \~$9    |
| 1 μs 蛋白质   | 25K 原子 | A100 80G | \~3天   | \~$55   |

***

## 更多资源

* [GROMACS 文档](https://manual.gromacs.org/)
* [GROMACS 教程（Justin Lemkul）](http://www.mdtutorials.com/gmx/)
* [NVIDIA HPC GROMACS 容器](https://catalog.ngc.nvidia.com/orgs/hpc/containers/gromacs)
* [GROMACS GitHub](https://github.com/gromacs/gromacs)
* [Amber 力场参数](https://ambermd.org/)
* [CHARMM-GUI 膜构建器](https://www.charmm-gui.org/)

***

*在 Clore.ai 上运行 GROMACS 使研究人员能够以远低于 AWS 或 Azure 的价格使用 A100 和 RTX 4090 GPU——让长时间的 MD 模拟在经济上对学术实验室和个人研究人员都可行。*

***

## Clore.ai GPU 推荐

| 使用场景         | 推荐 GPU         | Clore.ai 预计成本                     |
| ------------ | -------------- | --------------------------------- |
| 开发/测试        | RTX 3090（24GB） | $0.07–0.21/gpu/hr                 |
| 标准 MD 模拟     | RTX 4090（24GB） | $0.14–0.42/gpu/hr                 |
| 大型系统 / 长时间运行 | A100 80GB      | [裸机](https://clore.ai/bare-metal) |

> 💡 本指南中的所有示例都可以部署在 [Clore.ai](https://clore.ai/marketplace) GPU 服务器上。浏览可用 GPU 并按小时租用——无需承诺，拥有完整 root 访问权限。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.clore.ai/guides/guides_v2-zh/ke-xue-yu-yan-jiu/gromacs.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
