Help Running LAMMPS Input Script in C++ on Windows

I hope you’re doing well. I’m currently working on a project that involves running a LAMMPS input script from within a C++ program on Windows. I’ve been trying to figure it out on my own and have read through various resources, but I seem to have hit a dead end.

Issue: I’m struggling with incorporating the LAMMPS library into my C++ program and executing the input script. I’ve read the documentation and forums, but I’m still facing challenges.

What I’ve Tried:

  • Downloaded the LAMMPS source code from the official GitHub repository.
  • Attempted to link against the LAMMPS library in my C++ program.
  • Explored different forums and resources for solutions.

Specific Questions:

  1. How do I properly link and include the LAMMPS library in my C++ program on Windows?
  2. Are there any specific steps I should follow for running a LAMMPS input script from a C++ program?

I’m open to any guidance, suggestions, or examples that could help me overcome this hurdle. If you’ve successfully integrated LAMMPS into a C++ project on Windows or have experience with running LAMMPS input scripts programmatically, your insights would be invaluable.


Operating System: Windows
C++ executes on visual studio code

I appreciate any help or advice you can provide. Thank you!

Your life would be easier, if you installed the Windows Subsystem for Linux and you would compile and run your software on Linux (from within Windows). LAMMPS is primarily developed and tested on Linux.

Compiling LAMMPS on Windows is quite possible using a variety of compilers: GNU, Clang, Intel, and MS Visual C++. In either case you would be able to build a LAMMPS library and link to it.

So what is/are the error(s) you have encountered there?
Which compilers are you using? How did you include calling LAMMPS?

What solutions did you try and how did they fail?

There is guidance in the manual: 1. LAMMPS Library Interfaces — LAMMPS documentation
and there are examples in the LAMMPS source distribution: lammps/examples/COUPLE at b7376e34593d5512b8936ce1bb403e29dcdefa8c · lammps/lammps · GitHub

Unless you want to dig deep into the internals of LAMMPS, you want to use the C library interface, even if your program is written in C++. It has a much better documentation and in most cases would be functionally equivalent (you would otherwise basically just execute the code of the library interface directly).

This depends largely on how you have configured and compiled LAMMPS yourself and what compilers you are using. But there is also information in the manual: 3.3. Link LAMMPS as a library to another code — LAMMPS documentation

Please see the C library API documentation and the associated examples as well as the example sources provided with LAMMPS.

This will seem unrelated, but I encourage you to read this interesting and thought-provoking description of programming as theory-building, itself a reflection on an important essay about programming. The key insight is that

Programming isn’t about writing the code; it’s about understanding the problem and expressing that understanding through code.

Reading your first post, I am not sure what the problem is. I can tell that you tried to write a C++ program to do something, but it did something else instead. But I do not know:

  • what C++ program you wrote
  • what you expected it to do, and
  • what it actually did instead.

But even that isn’t the problem – the actual “problem” is whatever it is you are setting out to do. Do you need C++ to do it?? If you are on Windows, can you write a quick PowerShell loop to repeatedly call a LAMMPS executable precompiled for Windows on your series of scripts? Because, as another ancient coding cliche goes, sometimes the best code is no code at all.

If you can give us a high-level overview of what it is you’re trying to do, we can offer quick hints that might suggest more efficient and foolproof ways to accomplish it.