我正在尋找Fortran示例(也是接口函數)將數組作爲參數傳遞給lua函數。我能夠使用fortlua project開始。但提供的示例一次只傳遞一個元素。感謝任何幫助。將數組作爲函數參數傳遞給Fortran
--Lua code
local q1
local q2
function getoutput(qout1, qout2)
-- qout1 and qout2 are arrays with some dimension
q1 = qout1
q2 = qout2
end
- 在FORTRAN我用
config_function('getoutput', args, 2, cstatus)
但設置參數表是我在哪裏尋找一些幫助。我猜測下面的代碼是爲標量參數變量而不是數組做的。
!> Evaluate a function in the config file and get its result.
FUNCTION config_function(name,args,nargs,status)
REAL :: config_function
CHARACTER(LEN=*) :: name
REAL, DIMENSION(nargs) :: args
REAL(KIND=c_double) :: anarg
INTEGER :: nargs
INTEGER :: status
INTEGER :: iargs
INTEGER(c_int) :: stackstart
stackstart = lua_gettop(mluastate)
config_function = 0
status = 0
CALL lua_getglobal(mluastate,TRIM(name)//C_NULL_CHAR)
IF (lua_type(mluastate,-1) .eq. LUA_TFUNCTION) THEN
DO iargs = 1,nargs
anarg = args(iargs)
CALL lua_pushnumber(mluastate,anarg)
ENDDO
IF (lua_pcall(mluastate,nargs,1,0) .eq. 0) THEN
if (lua_isnumber(mluastate,-1) .ne. 0) THEN
config_function = lua_tonumber(mluastate,-1)
CALL lua_settop(mluastate,-2)
ELSE
! Nothing to pop here
status=-3
ENDIF
ELSE
CALL lua_settop(mluastate,-2)
status=-2
ENDIF
ELSE
CALL lua_settop(mluastate,-2)
status=-1
ENDIF
IF (stackstart .ne. lua_gettop(mluastate)) THEN
WRITE(*,*) 'The stack is a different size coming out of config_function'
ENDIF
END FUNCTION config_function
對所有Fortran問題使用標籤[tag:fortran]。 –
你可以將東西放到一個表中,然後將這個表傳遞給函數。但是,您仍然可能會將這些元素逐個推入表格中。我想,如果你正在研究大量的數據,使用userdata可能會更好,儘管這可能會相當複雜。在aotus中,我們也使用將單個標量放入堆棧,儘管您可以遍歷任意數量的元素:https://geb.sts.nt.uni-siegen.de/doxy/aotus/module/aot_fun_module.html – haraldkl