[lammps-users] LAMMPS as library and FORTRAN

Dear All,

I am trying to call lammps from a fortran code. Initially, since inside all the information that i want to collect from lammps can be collected through files, I am thinking of two alternatives:

  1. To call lammps as an external process from fortan however although I am not aware if fortran is capable of doing something like that.
  2. To build lammps as a library that can be used from the fortran code. For this case I have done the makelib and i have create the liblmp_g++.a but I don’t know how to continue from that point. I also tried unsuccessfully to run the couple example.

I would really appreciate your advice.

Best Regards,
Nick

Dear All,

dear nick,

I am trying to call lammps from a fortran code. Initially, since
inside all the information that i want to collect from lammps can be
collected through files, I am thinking of two alternatives:

1. To call lammps as an external process from fortan however although
   I am not aware if fortran is capable of doing something like that.

yes. you can compose the command line, that you want to execute
as a string and then use CALL SYSTEM(STRING). execution will
be suspended until this command finishes. if you are running in
parallel or want to run lammps in parallel, this can become somewhat
tricky.

2. To build lammps as a library that can be used from the fortran
   code. For this case I have done the makelib and i have create the
   liblmp_g++.a but I don't know how to continue from that point. I
   also tried unsuccessfully to run the couple example.

you cannot call c++ from fortran directly. what you'd need
is a wrapper library with c-style functions that follow the
naming can calling conventions of your fortran compiler.
have a look at the files library.h and library.cpp. those contain
already c bindings, AFAIK. you'd have to add similar interface
functions for fortran. if you want to run in parallel or use
parallel lammps, it can become a bit tricky, since in not all
cases can you use MPI communicators in fortran and c interchangeably.

i see a third alternative: write your program in c/c++!
it is not so difficult and you'll save a lot of headaches.

cheers,
   axel.

In examples/couple are a C++ and C file that call LAMMPS as a library.
Fortran would work exactly like the C file and pass strings (individual
commands) to LAMMPS, or it could call C subroutines that do
operations like in src/library.cpp. Driving LAMMPS as a library
is exactly the same from C and Fortran, except that if you pass
strings from Fortran, it can be a little tricky due to the different
way they are stored. I'd get the C example in couple to work, then
write a Fortran replacement for the driver program.

Steve