I have been working to add features to /fortran/lammps.f90 the last couple of days, and I have implementations all the way through lammps_extract_global (working through library.h in order) working and tested.
I thought I’d run this by Axel, at the very least, before I proceed much further. In the /examples/COUPLE/fortran2 API, I made lammps_extract_global and similar functions into subroutines whose first argument is the variable being returned. This isn’t identical to the C interface, but it’s close, and saves the trouble of having an array of extra constants around.
I came up with an alternative for /fortran/lammps.f90, which is to resolve what kind of data lammps_extract_global (from the C API) is returning similar to the way Python does it, namely to have another parameter called “dtype” whose sole duty is to state to the compiler that the function is returning an integer, a 64-bit integer, a 64-bit real number, a string, etc. For example,
integer (C_int) :: nlocal
real (C_double) :: dt
! more code
nlocal = lmp%extract_global('nlocal', LMP_INT)
dt = lmp%extract_global('dt', LMP_DOUBLE)
where LMP_INT and LMP_DOUBLE are (public) constants defined within /fortran/lammps.f90.
First, would doing it this way be confusing to users and/or a maintenance issue (or otherwise icky)?
This led me to thinking along the lines of how to deal with LAMMPS_BIGINT and the LAMMPS_SMALLBIG/LAMMPS_BIGBIG/etc. settings. Since those settings are all set by preprocessor directives, Fortran won’t see them at all, so there isn’t an easy solution (such as defining LMP_BIGINT=LMP_INT if bigints are ints and LMP_BIGINT=LMP_INT64 if bigints are int64_ts). We can pull the size of bigint, tagint, and imageint out of extract_setting once LAMMPS is running, but that doesn’t let the user declare something like
integer (C_bigint) :: ntimestep
at compile time (which is what they would do with the C or C++ APIs). Right now, I would lean toward having the Fortran API be agnostic to such things and rely on the user to “know” which integer kind to pass, but it would be nice to be “clever” about it (similar to the Python interface’s “dtype=LAMMPS_AUTODETECT” and subsequent type resolution, which of course is not possible in strongly-typed languages). Any thoughts?