Calling Fortran shared library in Julia always returns 0.0

Hello, everyone in the Julia discourse community, this is my first post here and I’m having a problem with calling Fortran in Julia.

The Fortran code I’m calling can be downloaded here, which is a scientific code.
What I changed is to add this function to the file adj8632_Code_S1.f90:

real*8 function pot_H2CO_V23(R, th1, th2, phi, rh2, rco)
    implicit none
    real*8,intent(in) :: R, th1, th2, phi, rh2, rco
    real*8 :: eint
    call pot_H2CO6D_V23(R, th1, th2, phi, rh2, rco, eint)
    pot_H2CO_V23 = eint
    return
end function pot_H2CO_V23

which is what I will be calling from Julia.

Then I compiled adj8632_Code_S1.f90 with
ifort -shared -fPIC adj8632_Code_S1.f90 -o adj8632_Code_S1.so, and then called from Julia:

function h2copes(R, θ1, θ2, ϕ, rh2, rco)
    eint = @ccall "path to library".:pot_h2co_v23_(R::Ref{Float64}, θ1::Ref{Float64}, θ2::Ref{Float64}, ϕ::Ref{Float64}, rh2::Ref{Float64}, rco::Ref{Float64})::Float64
    return eint
end
h2copes(8., 0., 180., 0., 1.474, 2.165)

now no matter what my input is, it always returns 0.0. And I don’t know what’s wrong.

I have used this approach with other Fortran codes, no problem has occured.

Have you verified that the fortran subroutine pot_H2CO6D_V23 receives the right input, and that eint is different from 0.0 (e.g. with a print *, or similar.

The input is correct and the Fortran function runs correctly in the Fortran code.

This is what’s eating at me: the function runs in the fortran file but not when called interoperably from Julia.

Try changing the return value into an output argument, i.e. change the function into a subroutine.

(I’ve had problems in the past with Fortran ABI calling conventions for function return values not matching C calling conventions; not sure if that’s an issue with ifort on your platform.)

1 Like

It turns out that I didn’t include the necessary parameters to run the subroutine pot_H2CO6D_V23 :sweat_smile:.

If you are interested, you need to call these subroutines:

call init_data_old
call init_data_new

they contain the parameters.

Thank you all for the responses, the Julia community is really helpful.

1 Like