How to link a 3rd party library when building lammps with cmake

A modification to lammps source code require the linear algebra library armadillo (makes it easier to translate from MATLAB). Since it is not needed in lammps by default, how to link this library when building lammps? I run lammps on WSL. Just trying to build lammps as is, results in lots of error. In the cmake documentation i have seen command for finding armadillo but how do i append it to cmakelist provided with lammps. Or is the approach totally different.

Sabik

There are loads of examples in the available LAMMPS CMake script code in the “cmake” tree.
Usually for additional source code, the recommended way would be to put it into a “package” folder under src and then have a file cmake/Modules/<Package>.cmake included when the package is enabled.

If you rather place your code/modifications in the src folder, you can look at how libraries to support JPEG, PNG, OpenMP, BLAS, LAPACK, ZLIB, as integrated into the build process. This is all standard CMake stuff, i.e. you use a compatible find_package() command, you may need to copy the corresponding Find<Package>.cmake to cmake/Modules. Then if it is found, there are either some variable or some imported targets that need to be added to target_link_libraries(lammps ...)

Thank you very much for the response. So i added the specific commands for finding armadillo and its dependencies to the basic.cmake preset, as i use that to build lammps with some extra packages.

Doing so resulted in the following error, when configuring cmake.
image
it stops with a segmentation fault. What can i do now.

Just to clarify, i am not making a new fix. Just extending the fix_ttm/mod have the source term bahave like a electromagnetic wave (to simulate laser irradiation better and its absorption). Which required complex matrix operation that i can do with the armadillo library easily. I have run the required code seperately that would run in each time step of MD simlation by compiling with gcc. It works now i have merged it with lammps. However i am stuck at building. I just need lammps to pick up the header files.

Sabik

That is a very, VERY bad idea. Since the preset is loaded before everything else, you cannot associate the library with a target, since no targets exist at that point yet.
You have to modify cmake/CMakeLists.txt and put the change somewhere near where other libraries are added.

You seem to have triggered a bug in your CMake version. If it does segfault, however, it should reject your code in the preset file, since it is invalid to do as I explained above.

If you change it that way, the natural way to do it would be to create a new command as a derived class and just override the bits and pieces you need to be different. As it stands, there is no chance that such a change could ever be contributed back to the LAMMPS distribution.

If you need matrix operations, why not use LAPACK? There is a whole set of LAPACK functions included in LAMMPS in the lib/linalg folder that can be compiled with a C++ compiler (they’ve been translated by f2c plus a little “extra magic™”) and thus are always available, even if there is no local LAPACK installation.

Thank you so much, Axel. Its working now.


The calculated reflectivity of the laser irradiated sample is also matching my results from MATLAB.
This implementation of the fix will have a better replication of laser pulse in simulating ablation phenomenon. I am eager to add this modification as a seperature fix in lammps. I still have to add functionality for multipulse laser. But you said that with my current approach i cant contribute the modification to lammps distribution. I would like know further about how i should proceed if i want this to be a seperate fix in the future which is derivative of the available fix ttm/mod.

Sabik

And if someone else face similar issue, this is what I did to add the armadillo library. I looked for the portion of cmakelists.txt where other libraries are added. I edited like the following just beside the LAPACK and BLAS

What you did so far should be considered a “hack”. It is not acceptable for integrating into the distribution as it will add a required global dependency on the armadillo library for an optional feature. Thus, to contribute this code back in this form, it must be set up as a separate package and the library dependency thus would only be required when the package is installed. This would also require corresponding work to support this for both the CMake build procedure as well as the conventional make build procedure. You have to look at existing packages and their library dependencies and how this is resolved there. As I was referring you before, the barrier for inclusion is going to far lower, if you would rewrite your code to use LAPACK directly. Then you can latch on to the existing infrastructure for linking to it. That would also lower the barrier for people that might be interested to use your code, since they would have to download, configure and install another library, especially since that library sits on top of LAPACK, anyway.

The general discussion of how to contribute and what the requirements are is in the LAMMPS manual here: 3. Modifying & extending LAMMPS — LAMMPS documentation