Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 168 additions & 0 deletions content/notes/compile-gmx-with-cp2k.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
---
title: Compiling Gromacs with CP2K for QM/MM simulation
description: Recording the process of compiling Gromacs for QM/MM simulation with CP2K
date: 2025-06-15T18:49
update: 2025-06-16T00:01
tags:
- note/2025/06
- note/molecular-dynamics
id: note20250615184922
dg-publish: true
maturity: tree
---
> [!quote] Hybrid Quantum-Classical simulations (QM/MM) with CP2K interface
> In a molecular mechanics (MM) force field, the influence of electrons is expressed by empirical parameters that are assigned on the basis of experimental data, or on the basis of results from high-level quantum chemistry calculations. These are valid for the ground state of a given covalent structure, and the MM approximation is usually sufficiently accurate for ground-state processes in which the overall connectivity between the atoms in the system remains unchanged. However, for processes in which the connectivity does change, such as chemical reactions, or processes that involve multiple electronic states, such as photochemical conversions, electrons can no longer be ignored, and a quantum mechanical description is required for at least those parts of the system in which the reaction takes place. One approach to the simulation of chemical reactions in solution, or in enzymes, is to use a combination of quantum mechanics (QM) and molecular mechanics (MM). The reacting parts of the system are treated quantum mechanically, with the remainder being modeled using the force field. The current version of GROMACS provides an interface to the popular Quantum Chemistry package CP2K [^1]

# Preparing CP2K interface for QM part
Integrating the CP2K QM/MM interface into GROMACS will require linking against the libcp2k library, which incorporates CP2K functionality.

> [!Info] Notice
> CP2K version `8.1` or higher and Gromacs version `2022` or higher are requred. Here I used CP2K `2024.1` and Gromacs `2024.2`.

## Dependencies preparation
### OpenMPI
Although CP2K can automatically install OpenMPI, I opted to compile it manually to ensure compatibility with the specific OpenMPI version required by other software.

```bash
wget https://download.open-mpi.org/release/open-mpi/v4.1/openmpi-4.1.1.tar.bz2
tar -xvf openmpi-4.1.1.tar.bz2
cd openmpi-4.1.1/
./configure --prefix=/opt/openmpi411 --disable-builtin-atomics
sudo make -j 12 install
```

Note that the installation directory, such as `/opt/openmpi411`, can be located anywhere you prefer.

After installation, add `openmpi` library into `PATH` by editting `~/.bashrc` :
``` bash ~/.bashrc
export PATH=/opt/openmpi411/bin:$PATH
export LD_LIBRARY_PATH=/opt/openmpi411/lib:$LD_LIBRARY_PATH
```

Note that after each time you edit the `.bashrc` file, don't forget to apply the changes:
```bash
source ~/.bashrc
```

### FFTW
If your CPUs support AVX2, it is recommended to compile the `fftw` library yourself using the `--enable-avx2` option to accelerate the simulation process.

``` bash
wget http://www.fftw.org/fftw-3.3.10.tar.gz
tar -zxf fftw-3.3.10.tar.gz
cd fftw-3.3.10/
./configure --prefix=/opt/fftw3310 --enable-mpi --enable-openmp --enable-sse2 --enable-avx --enable-float --enable-shared --enable-avx2
sudo make -j 12 install
```

Similarly, that the installation directory `/opt/fftw3310` can be located anywhere you prefer.

After installation, add `fftw` library into `PATH` by editting `~/.bashrc` :
``` bash ~/.bashrc
export PATH=/opt/fftw3310/bin:$PATH
export LD_LIBRARY_PATH=/opt/fftw3310/lib:$LD_LIBRARY_PATH
```

## Compiling CP2K
Here, I use CP2K version `2024.1`. The components used for compiling CP2K should be compatible with those of Gromacs.

Install CP2K with `toolchain`:
``` bash
cd /path/to/cp2k/
cd tools/toolchain/
./install_cp2k_toolchain.sh \
--with-libxsmm=install \
--with-elpa=install \
--with-libxc=install \
--with-libint=install \
--with-gsl=no \
--with-libvdwxc=no \
--with-spglib=no \
--with-hdf5=no \
--with-spfft=no \
--with-cosma=no \
--with-libvori=no \
--with-sirius=no \
--with-scalapack=install \
--with-openblas=install \
--with-fftw=/opt/fftw3310 \
--with-openmpi=system \
--with-plumed=install
```

This process will take tens of minutes. You can also utilize previously installed components in your system, such as by specifying `--with-openblas=system`.

Note that the location of FFTW (/opt/fftw3310, or other location you specified when installing FFTW) must be specified; otherwise, errors will occur during the compilation of Gromacs.

After this process, all components installed by `toolchain` can be found in `/path/to/cp2k/tools/toolchain/install/`.

Then we can complie CP2K:
``` bash
cp install/arch/* ../../arch/
source install/setup
make -j 12 ARCH=local VERSION="ssmp psmp"
make -j 12 ARCH=local VERSION="ssmp psmp" libcp2k
```

If the compilation succeeds, you should find the executable files in `/path/to/cp2k/exe/`, and the `libcp2k.a` file in `/path/to/cp2k/lib/psmp/`.

Add CP2K into `PATH` by editting `~/.bashrc`:
```bash ~/.bashrc
source /path/to/cp2k/tools/toolchain/install/setup
export PATH=$PATH:/path/to/cp2k/exe/local
export CP2K_DATA_DIR=/path/to/cp2k/data
```

# Compiling Gromacs
Here, I use Gromacs version `2024.2`. The compilation process for Gromacs is much simpler than that for CP2K, as most of the dependencies have already been prepared during the CP2K compilation.

``` bash
cd /path/to/gromacs
mkdir build
cd build\
cmake .. -DCMAKE_PREFIX_PATH=/opt/fftw3310 \
-DCMAKE_INSTALL_PREFIX=/path/to/gromacs/ \
-DBUILD_SHARED_LIBS=OFF \
-DGMXAPI=OFF \
-DGMX_INSTALL_NBLIB_API=OFF \
-DGMX_DOUBLE=ON \
-DGMX_MPI=ON \
-DGMX_FFT_LIBRARY=fftw3 \
-DGMX_BLAS_USER=/path/to/cp2k/tools/toolchain/install/openblas-0.3.25/lib/libopenblas.so \
-DGMX_LAPACK_USER=/path/to/cp2k/tools/toolchain/install/openblas-0.3.25/lib/libopenblas.so \
-DGMX_CP2K=ON \
-DCP2K_DIR=/path/to/cp2k/lib/local/psmp \
-DGMX_DEFAULT_SUFFIX=OFF \
-DCP2K_LINKER_FLAGS="..." \
-DMPI_C_COMPILER=/opt/openmpi411/bin/mpicc \
-DMPI_C_COMPILER=/opt/openmpi411/bin/mpicxx \
-DMPI_Fortran_COMPILER=/opt/openmpi411/bin/mpif90
```

`-DGMX_BLAS_USER` and `-DGMX_LAPACK_USER` options specify the library for `BLAS` and `LAPACK`. `libopenblas` provides both of them.

`-DCP2K_LINKER_FLAGS` option specifies libraries which should be compatible with those used for the compilation of CP2K. Specifically, you can find them in the `/path/to/cp2k/tools/toolchain/install/arch/local.psmp` file:
```
LDFLAGS = $(FCFLAGS) ...
LIBS = ...
```

`-DMPI_C_COMPILER`, `-DMPI_C_COMPILER` and `-DMPI_Fortran_COMPILER` specify comilers used for compilation. You should specify them if there're multiple compilers for MPI compiling.

Compile Gromacs:
```bash
make -j 12
sudo make -j 12 install
```

You should find the executable files in `/path/to/gromacs/bin` if the compilation succeeds.

Add Gromacs into `~/.bashrc`:
```bash
source /path/to/gromacs/bin/GMXRC
export PATH=$PATH:/path/to/gromacs/bin
```

Enjoy QM/MM simulation with Gromacs and CP2K!

[^1]: See Gromacs Manual "[Hybrid Quantum-Classical simulations (QM/MM) with CP2K interface](https://manual.gromacs.org/current/reference-manual/special/qmmm.html)"
2 changes: 1 addition & 1 deletion content/readings/existentialisme-est-un-humanisme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ title: 存在主义是一种人道主义
author: "[法]让-保罗·萨特"
cover: https://cdn.freezing.cool/images/202403051223853.jpg
date: 2024-03-05T12:20
update: 20254-6013-009085T2::2221:53
update: 2025-06-15T22:35
douban: https://book.douban.com/subject/10608319/
dg-publish: true
maturity: raindrop
Expand Down
2 changes: 1 addition & 1 deletion content/readings/in-search-of-tashui-village-a-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ tags:
author: 沈至
cover: https://cdn.freezing.cool/images/202402171426515.jpg
date: 2024-02-14T20:11
update: 2024-03-098T2:2230:18
update: 2025-06-15T22:34
douban: https://book.douban.com/subject/36527880/
dg-publish: true
maturity: tree
Expand Down
4 changes: 2 additions & 2 deletions content/readings/the-blind-assassin.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ tags:
author: "[加]玛格丽特·阿特伍德"
cover: https://cdn.freezing.cool/images/202402171426022.jpg
date: 2024-02-15T22:23
update: 2024-03-09T32:2T2414:19
update: 2025-06-15T22:34
douban: https://book.douban.com/subject/26748179/
dg-publish: true
maturity: raindrop
Expand All @@ -25,7 +25,7 @@ title: 盲刺客

# After Reading

>[!quote]我想从你那里得到什么呢?
>[!quote] 我想从你那里得到什么呢?
>不是爱,因为这个要求过分了。不是原谅,因为那不是你能赐予的。或许只是一名听众,只是一个愿意看望我的人。不过,无论你还要做什么,不要美化我;我并不希望做一具装饰过的颅骨。[^1]

[^1]: 摘自玛格丽特·阿特伍德《盲刺客》
2 changes: 1 addition & 1 deletion content/readings/the-continuing-silence-of-a-poet.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ tags:
author: "[以色列]亚伯拉罕·耶霍舒亚"
cover: https://cdn.freezing.cool/images/202402261313962.jpg
date: 2024-02-26T13:10
update: 2024-03200022-2422242974T4050:23
update: 2025-06-15T22:35
douban: https://book.douban.com/subject/35552618/
dg-publish: true
maturity: raindrop
Expand Down