I just sent in some modifications to the Fortran interface that should now
make it more or less the same in terms of functionality to the C
interface. The previous version used 100% Fortran-native data types,
allocatable arrays, and copies of LAMMPS data, which made it much easier
to get started for those not familiar with C. This meant it was much
harder, however, to manipulate data (via lammps_extract_atom, for
example), as you then had to copy the data back somehow before LAMMPS
would notice them.
The example (simple.f90) and the README have been updated to reflect these
So here's what's new:
* You should declare data that come directly from LAMMPS as
C-interoperable data. Thus INTEGER (C_int) and REAL (C_double) instead
of INTEGER and DOUBLE PRECISION. On most compilers, these are the same
thing, but to ensure portability you should use the ISO_C_BINDING
* The first argument (analogous to the return value in the C interface)
must now be a C interoperable POINTER (not an allocatable). For
integer (C_int), pointer :: types(
! more code, incl. lammps_open call
call lammps_extract_atom (type, lmp, 'type')
will associate the pointer 'types' with the atom types.
* You could, in principle, change said types from your Fortran program,
and the changes are made to LAMMPS directly. (In the previous
iterations of this interface, 'type' would have been a copy of the
type data, not the real thing)
* lammps_scatter_atoms and lammps_gather_atoms do the same thing they
did before, with the same arguments. Note that they take allocatable
arrays, not pointers, as arguments, and those arguments are integer or
double precision (not integer(C_int) or real (C_double)).
* lammps_extract_global takes its first argument as a scalar pointer;
this allows values such as the box lengths to update
* lammps_extract_variable still returns copies of the data, just as the
C interface does.
* The i and j values and their order for lammps_extract_variable is the
same as it is in LAMMPS proper; this is unchanged (cf. next bullet)
* IMPORTANT: Consider this example:
real (C_double), dimension(:,:), pointer :: x
! more code
call lammps_extract_atom (type, lmp, 'x')
print *, 'shape (x) = ', shape (x)
The above code will print "3 N" where N is the number of atoms. This
is the /transpose/ of what we might think of as the same array in
C/C++. The previous version tried to preserve shape (i.e., x(2,1)
was the same as x), but that is not possible when dealing with
the original data. This transposition will occur for all 2D arrays.
This last point is a consequence of the different bookkeeping convention
(row-major vs. column-major ordering) that the C language chose back in
the 1970's (Fortran, of course, chose the opposite convention about 20
years earlier). It's sort of like the carriage-return (Apple) vs.
line-feed (UNIX) vs. both (DOS/Windows) problem that exists with ASCII
text files...just something to be aware of.
Karl D. Hammond