DEV Community

loading...
Cover image for How to debug your LAMMPS simulation using CLion

How to debug your LAMMPS simulation using CLion

jkrajniak profile image Jakub Krajniak ・3 min read

From time to time I need to debug LAMMPS code to find out why a simulation does not work as expected. The easiest way is to jump into the runtime using a debugger. Although gdb does the job, it is not the most intuitive and easy to use the tool.

In my daily work, whenever I can, I use JetBrains toolbox, for C++ programming - CLion. It definitely simplifies work, allowing as well to easy debug the code.

LAMMPS uses CMake to control the build process. Fortunately for us, CLion integrates with CMake projects very well.

So let's start.

Setup project

  1. Open CLion, if you end up in the Welcome Window then click on Open welcome-screen and select the root of LAMMPS directory.

select-project
If you end up in the main window with an already opened project, you can go to Open ➡️ File ➡️ Open... and select the root of LAMMPS directory.

2. To enable CMake project, open cmake/CMakeLists.txt. You should see in the editor window on the top link to Load CMake project, click on it.
Alt Text

That is it, now you can compile your LAMMPS code directly from CLion, run and debug.

Enable/disable different compilation options

LAMMPS contains multiple packages, which enables various of capabilities. This modular design allows compiling the final binary executables with only these functions that are needed to simulate your system.
You can enable/disable features and settings by -D argument. The other, more convenient option, is to use CMake presets, which contains predefined variables.

The preset can be enabled from command line:

$ mkdir build; cd build
$ cmake -C ../cmake/presets/minimal.cmake ../cmake
Enter fullscreen mode Exit fullscreen mode

and to enable multiple one at the same time

$ cmake -C ../cmake/presets/nolib.cmake -C ../cmake/presets/gcc.cmake -C ../cmake/presets/most.cmake  ../cmake
Enter fullscreen mode Exit fullscreen mode

You can attach the same settings in the Clion project configuration by putting CMake command line option in CMake options field.
Alt Text

Run your simulation script

Now, when we set up our project, we can run our simulation script.

The easiest way is to make a copy of an existing configuration and include the simulation script.
Go to Run ➡️ Edit Configurations...
Alt Text

In the list of targets, search for lmp and click on Copy icon, change the name of the target.
Next, set the Working directory to the path where your simulation scripts are located. In the Program arguments you can specify which simulation script to run, e.g. -in in.lmp if your simulation script is in in.lmp file.

Run debugger

Running debugger is pretty simple. In the main window select the target to run, and click on the Debug icon.
Alt Text


I have guided you step by step on how to set up debugging for LAMMPS package. However, you can debug your GROMACS or ESPResSo++ simulations.


Happy debugging!

Discussion (0)

pic
Editor guide