Extend or continue a GROMACS simulation

 

When dealing with molecular simulations it is very likely that at some point you will find yourself in one of the following situations:

  1. The simulation stops and you don’t want to restart it from the beginning. You would rather start it from some point before the crash so that you can avoid wasting computational time.

  2. The simulation finishes but you realize that you would like to extend it for additional time.

Going through this can be quite annoying especially if you are a beginner. In this article, I will teach you how you can continue or extend a GROMACS simulation. This will save you a lot of time and headaches.

Let’s see how to do it in practice.

 

 

If for some reason your simulation stopped, and you want to restore it, don’t worry.

By default, GROMACS writes a checkpoint file of your system in the cpt format (md.cpt) every 15 minutes. It also automatically backs up the previous checkpoint as md_prev.cpt.

The checkpoint files contain a complete description of the system storing the coordinates and velocities of the system at full precision. Therefore, you can always continue the simulation from the last checkpoint exactly as if the interruption never occurred.

To do that, you only need to pass it to the gmx mdrun with the -cpi flag. Here is an example:

 

1
gmx mdrun -v -deffnm md -cpi md.cpt (-cpt 5)
Syntax
  1. Call the program with gmx
  2. Select the mdrun command
  3. The -v makes the command verbose. It is useful to make clearer what the program is actually doing while running.
  4. The -deffnm option is followed by the prefix of the tpr file we are using. This will generate files having the same prefix.
  5. We will continue the simulation from the md.cpt provided via the -cpi flag.
Optional
  1. You can change the frequency of the checkpoint output. -cpt 5 will write a checkpoint file every 5 minutes. Just in case.

 

The simulation will proceed from the checkpoint you selected until the end. The total simulation time will be the one you specified in the original mdp file you used to create the tpr file.

Note that if you restart different simulations from the same checkpoint you will find that the continuations will diverge from each other. This is due to the limited precision of computers at our disposal. In principle, you should be able to exactly reproduce the same results with an optimal computer having unlimited precision.

Despite this, different trajectories are all equally valid and none of them is better than the others. So don’t worry about this.

 

 

Another possible problem you may encounter regards the extension of a simulation.

In some cases, you may run a simulation for a certain period of time but once it finishes you decide that you want to proceed further. To accomplish this you can use the built-in gmx convert-tpr module.

This module allows you to edit the tpr files you assembled via the gmx grompp module in various ways. Among others, we can decide to extend the duration of the simulation for a certain tpr file.

This procedure involves two steps:

  1. First you need to extend the duration of the simulation. We will do this with the gmx convert-tpr module that will generate a new tpr file with additional time.
  2. In the second step you will only need to run the new tpr file starting from the last checkpoint (as explained above).

 

 

 

Let’s see a practical example to make things more clear.

Let’s say you finished a 10 ns simulation from a tpr file (md_10.tpr) but then you realized you would like to simulate your system for an additional 10 ns.

You can use the gmx convert-tpr as follows:

1
gmx_mpi convert-tpr -s md_10.tpr -extend 10000 -o md_20.tpr (-nsteps) (-until)
Syntax
  1. Call the program with gmx
  2. Select the convert-tpr command
  3. Choose the -s flag and provide the original tpr file you want to extend (md.tpr).
  4. The -extend option is followed by the number of ps you want to add to your simulation. In our case, we want to add 10 ns (10 000 ps).
  5. Call the -o flag and decide how you want to name the new tpr file (md_20.tpr)
Optional
  1. You can also decide to extend the simulation for a given number of steps (10 steps: -nsteps 10) or until a certain ending time in ps (100 ns ending time: -until 100000). With -nsteps -1 you will get an infinite number of steps so that you can decide when to stop the simulation.

 

Now we can run the new tpr file (md_20.tpr) starting from the last checkpoint.

1
gmx mdrun -v -deffnm md_20 -cpi md_10.cpt -noappend

 

You will get a trajectory with the new designation (md_20) instead of the old one (md_10) and the corresponding files (md_20.xtc, md_20.edr, …).

You can finally proceed to concatenate the new trajectory to the previous one using the gmx trjcat module as shown here.

Something like:

1
gmx trjcat -f md_10.xtc md_20.part0002.xtc -o final.xtc

 

The final.xtc trajectory contains the entire simulation of your system.

You can repeat this procedure as many times as you wish. In the end, you can concatenate all the trajectories as follows:

1
gmx trjcat -f *.xtc -o final.xtc

 

GROMACS will automatically order all the trajectory files in the directory and then proceed to link them together.

 

 

If you want to extend the trajectory and append it to the existing one the process is a little bit different.

Let’s consider a generic simulation that you started using a file named md.tpr.

The first thing to do is to create a backup of your existing trajectory so that in case something goes wrong you will not lose all your data. Backup the entire directory.

Then, you need to use the convert-tpr module to extend the simulation time without creating a new tpr file. In other words, the name of the tpr file that you create via the -o flag needs to be the same as the one you already used.

To extend it for an infinite amount of time you can use the -nsteps -1 flag, then it will be up to you to stop the simulation when you think you don’t need additional time.

1
gmx_mpi convert-tpr -s md.tpr -nsteps -1 -o md.tpr 

 

Now you have a new md.tpr file that you can run for an infinite number of steps.

To append to the previous files you will need to have all the files generated by the previous simulation with the same prefix in the current directory (md.xtc, md.edr, md.log, md.trr, md.cpt). Note that you can only append if all the files have the same designation.

Finally, you can run the simulation starting from the last cpt file:

1
gmx mdrun -v -deffnm md -cpi md.cpt