TREX-CoE / trexio

TREX I/O library
https://trex-coe.github.io/trexio/
BSD 3-Clause "New" or "Revised" License
49 stars 14 forks source link

Fortran interface to write reciprocal lattice vectors is broken #156

Closed stefabat closed 6 months ago

stefabat commented 6 months ago

Because Fortran is case insensitive, calls to trexio_write_cell_G_a(trex_file, dset) (the same for G_b and G_c) get translated to trexio_write_cell_g_a(trex_file, dset) and are not found the in the C library. The latter is case sensitive, and thus the call should be to trexio_write_cell_G_a(trex_file, dset).

Steps to reproduce the issue: Compiling the simple program

program test_trexio                                                                                                                 
  use trexio
  integer (trexio_t) :: trexio_file
  integer (trexio_exit_code) :: rc
  trexio_file = trexio_open('test_file', 'w', TREXIO_TEXT, rc)
  rc = trexio_write_cell_G_a(trexio_file, (/0.d0,0.d0,0.d0/))
  print *, rc
end

with gfortran /opt/trexio/include/trexio_f.f90 test_trexio.F90 -L /opt/trexio/lib -ltrexio results in the following error:

/usr/bin/ld: /tmp/ccJ8F9mb.o: in function `MAIN__':
test_trexio.F90:(.text+0xd9): undefined reference to `trexio_write_cell_g_a'
collect2: error: ld returned 1 exit status

To fix it, one simply needs to add the name= argument to the bind(C) directive in the function signature in the Fortran interface. That is, one has to change

interface
   integer(trexio_exit_code) function trexio_write_cell_G_a (trex_file, dset) bind(C)
     import
     integer(trexio_t), intent(in), value :: trex_file
     real(c_double), intent(in) :: dset(*)
   end function trexio_write_cell_G_a
end interface

to

interface
   integer(trexio_exit_code) function trexio_write_cell_G_a (trex_file, dset) bind(C, name='trexio_write_cell_G_a')
     import
     integer(trexio_t), intent(in), value :: trex_file
     real(c_double), intent(in) :: dset(*)
   end function trexio_write_cell_G_a
end interface

in trexio_f.f90.

I looked into src/templates_front/templator_front.org to change it myself and make a PR, however, these functions are generated automatically based on a fix template and I do not know the syntax for inserting a special case.

Very likely, all functions in the interface containing capital letters in their name will require the same modification, such that a regular expression could be used to identify the cases where it is necessary to explicitly add the name= argument to bind(C).

scemama commented 6 months ago

Good catch! The function names should all be lowercase. It will be easier. Thanks! I will fix it.

stefabat commented 6 months ago

I am not sure effc14c fixed it. From a fresh clone of the repo, and rebuilding everything from scratch, I still see the capital letters in the function names.

q-posev commented 6 months ago

That's because @scemama forgot to regenerate the JSON part of the code from the modified table, it's an easy fix if you have emacs installed (Ctrl-C Ctrl-C)

scemama commented 6 months ago

That's because @scemama forgot to regenerate the JSON part of the code from the modified table, it's an easy fix if you have emacs installed (Ctrl-C Ctrl-C)

Fixed in 05b943