[UP]


Manual Reference Pages  - M_lua (3)

NAME

M_lua(3fm) - [M_lua] Fortran interface to the LUA language (LICENSE:MIT)

CONTENTS

Synopsis
Description
Example

SYNOPSIS

DESCRIPTION

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).

    MOTIVATION

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.

    COMPILATION

This project assumes that the Lua library is available on your system.

    DISCUSSION

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. It’s 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 Fortran’s 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.com

This modification to FortLua was derived from routines provided by AOTUS, hence their attribution is listed below:

    AOTUS LICENSE

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.

EXAMPLE

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

    ELSE

WRITE (*,*) ’error reading temperature’

    ENDIF

newval = config_integer(’newval’,read_status) IF ( read_status .eq. 0 ) THEN WRITE (*,*) ’newval = ’, newval

    ELSE

WRITE (*,*) ’error reading newval’

    ENDIF

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

    END DO

CALL config_close()

    STOP

END PROGRAM calculate

Input file

   -- vals.lua
   -- This is a configuration file for the Fortran program.
   -- Lua isn’t 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


M_lua (3) October 17, 2020
Generated by manServer 1.08 from 8f473a93-de6f-458b-b842-135c64817ca1 using man macros.