M_lua(3fm) - [M_lua] Fortran interface to the LUA language (LICENSE:MIT)
Synopsis
Description
Example
This is a fork of a simple set of Lua bindings for use in Fortran, tested to be compatible with Lua 5.2.3+, based on FORTLUA 5.2.
Most of the original basis work is fully attributed to @adolgert and @adambrozier and creators of Aotus (see attribution clause below).
This project shows how to call a Lua file from Fortran. You can also call a Lua file from C, but doing it from an even older language seems more dramatic. This also demonstrates how to call C libraries directly from Fortran using the iso_c_binding.
This project assumes that the Lua library is available on your system.
Why call Lua from Fortran? I can think of two good uses for this.
You could use a Lua file as a configuration file. You can query environment variables and do pre-calculations in the configuration file, yielding a result for Fortran to use. You can even pass in functions for Fortran to execute.
You could also write a commonly-modified subroutine in Lua so that there is no need to recompile the code when you make changes.
Why Lua? The language is relatively simple and has basic math included. Its made to embed in programs. That said, it is not a common language. You could do the same exercise with Gnu Guile.
How? Lua is a very small language that is built to be embedded. It passes all of its state back to the calling program through a stack (the stack data structure, but stored on the heap) so that it is simple to retrieve.
We use Fortrans iso_c_binding to call the Lua C library directly from Fortran. That binding is a relatively new development in Fortran, but it works fine.
Drew Dolgert adolgert@cornell.edu Adam Brazier brazier@cornell.edu Kevin Manalo kmanalo@gmail.comThis modification to FortLua was derived from routines provided by AOTUS, hence their attribution is listed below:
Aotus is licensed under the terms of the MIT license reproduced below. This means that Aotus is free software and can be used for both academic and commercial purposes at absolutely no cost. You are free to do with the code whatever you want. The only requirement is that some credit to the authors is given by putting this copyright notice somewhere in your project. The MIT license is chosen for full compatibility with Lua.
For the license of the underlying Lua library have a look at
http://www.lua.org/license.html.
Sample program
! This program uses a script to configure itself.PROGRAM calculate USE M_lua
INTEGER :: argument_count REAL*8 :: pressure REAL*8 :: time INTEGER :: step, stepcount INTEGER :: status REAL :: temperature_start REAL, DIMENSION(1) :: pressure_args INTEGER :: read_status INTEGER :: cstatus integer :: newval
character(50) :: string character(:), allocatable :: astring
status = config_open(vals.lua)
call config_string(string,read_status, string) astring = trim(string)
write(*,*) string = , astring
temperature_start = config_real(temperature,read_status) IF ( read_status .eq. 0 ) THEN WRITE (*,*) temperature = , temperature_start
WRITE (*,*) error reading temperature
newval = config_integer(newval,read_status) IF ( read_status .eq. 0 ) THEN WRITE (*,*) newval = , newval
WRITE (*,*) error reading newval
stepcount=10
DO step = 1,stepcount time = step/1.0 pressure_args(1) = time pressure=config_function(pressure,pressure_args,1,cstatus) WRITE (*,*) pressure = , pressure
CALL config_close()
END PROGRAM calculate
Input file
-- vals.lua -- This is a configuration file for the Fortran program. -- Lua is not too complicated. Check out -- http://lua-users.org/wiki/TutorialDirectory-- Parameters if os.getenv("BATCH") then temperature=3.2 else temperature=5.0 end
mintemp=3 maxtemp=6 duration=10 -- time to get to 95% string=10 -- string will be interpreted because that is what this program is looking for newval=10.5
function pressure(time) return mintemp+(maxtemp-mintemp)*math.tanh(1.83*time/duration) end
Nemo Release 3.1 | M_lua (3) | February 23, 2025 |