forked from lijiext/lammps
84 lines
3.5 KiB
Plaintext
84 lines
3.5 KiB
Plaintext
This directory has a simple C, C++, and Fortran code that shows how
|
|
LAMMPS can be linked to a driver application as a library. The purpose
|
|
is to illustrate how another code could perform computations while
|
|
using LAMMPS to perform MD on all or a subset of the processors, or
|
|
how an umbrella code or script could call both LAMMPS and some other
|
|
code to perform a coupled calculation.
|
|
|
|
simple.cpp is the C++ driver
|
|
simple.c is the C driver
|
|
simple.f90 is the Fortran driver
|
|
libfwrapper.c is the Fortran-to-C wrapper
|
|
|
|
The 3 codes do the same thing, so you can compare them to see how to
|
|
drive LAMMPS from each language. See lammps/python/example/simple.py
|
|
to do something similar from Python. The Fortran driver requires an
|
|
additional wrapper library that interfaces the C interface of the
|
|
LAMMPS library to Fortran and also translates the MPI communicator
|
|
from Fortran to C.
|
|
|
|
First build LAMMPS as a library (see examples/COUPLE/README), e.g.
|
|
|
|
make mode=shlib mpi
|
|
|
|
You can then build any of the driver codes with compile lines like
|
|
these, which include paths to the LAMMPS library interface, and
|
|
linking with FFTW (only needed if you built LAMMPS as a library with
|
|
its PPPM solver).
|
|
|
|
This builds the C++ driver with the LAMMPS library using the mpiCC
|
|
(C++) compiler:
|
|
|
|
mpiCC -I/home/sjplimp/lammps/src -c simple.cpp
|
|
mpiCC -L/home/sjplimp/lammps/src simple.o -llammps -lfftw -o simpleCC
|
|
|
|
This builds the C driver with the LAMMPS library using the mpicc (C)
|
|
compiler:
|
|
|
|
mpicc -I/home/sjplimp/lammps/src -c simple.c
|
|
mpicc -L/home/sjplimp/lammps/src simple.o -llammps -lfftw -o simpleC
|
|
|
|
This builds the Fortran wrapper and driver with the LAMMPS library
|
|
using the mpicc (C) and mpifort (Fortran) compilers, using the wrapper
|
|
in the fortran directory:
|
|
|
|
cp ../fortran/libfwrapper.c .
|
|
mpicc -I/home/sjplimp/lammps/src -c libfwrapper.c
|
|
mpifort -c simple.f90
|
|
mpifort -L/home/sjplimp/lammps/src simple.o libfwrapper.o \
|
|
-llammps -lfftw -o simpleF
|
|
|
|
You then run simpleCC, simpleC, or simpleF on a parallel machine
|
|
on some number of processors Q with 2 arguments:
|
|
|
|
% mpirun -np Q simpleCC P in.lj
|
|
|
|
P is the number of procs you want LAMMPS to run on (must be <= Q) and
|
|
in.lj is a LAMMPS input script.
|
|
|
|
The driver will launch LAMMPS on P procs, read the input script a line
|
|
at a time, and pass each command line to LAMMPS. The final line of
|
|
the script is a "run" command, so LAMMPS will run the problem.
|
|
|
|
The driver then requests all the atom coordinates from LAMMPS, moves
|
|
one of the atoms a small amount "epsilon", passes the coordinates back
|
|
to LAMMPS, and runs LAMMPS again. If you look at the output, you
|
|
should see a small energy change between runs, due to the moved atom.
|
|
|
|
The C driver is calling C-style routines in the src/library.cpp file
|
|
of LAMMPS. You could add any functions you wish to this file to
|
|
manipulate LAMMPS data however you wish.
|
|
|
|
The Fortran driver is using the same C-style routines, but requires an
|
|
additional wrapper to make them Fortran callable. Only a subset of the
|
|
library functions are currently wrapped, but it should be clear how to
|
|
extend the wrapper if desired.
|
|
|
|
The C++ driver does the same thing, except that it instantiates LAMMPS
|
|
as an object first. Some of the functions in src/library.cpp can be
|
|
by-passed and invoked directly as methods within appropriate LAMMPS
|
|
classes, which is what the driver does. Any public LAMMPS class
|
|
method could be called from the driver this way. However the get/put
|
|
functions are only implemented in src/library.cpp, so the C++ driver
|
|
calls them as C-style functions.
|