The field and particle trajectory simulator
Industry standard charged particle optics software
Refine your design: model > simulate > view > analyze > program > optimize it.
SIMION v8.1.3.0-TEST posted.
About | Documentation | Community/Support | Downloads | Ordering

Lua Cookbook

This page contains examples of common tasks performed in Lua under SIMION 8.0 or 8.1.

Loading/saving array from a text file

Method 1

-- test.dat:
return {
  3, 1, 4,
  1, 5, 9

-- test.lua:
local array = dofile("test.dat")
-- print contents
for i,v in ipairs(array) do


1       3
2       1
3       4
4       1
5       5
6       9

Above, test.dat is actually a Lua program itself that returns an array. The test.lua program runs the test.dat program to obtain the array it returns.

Method 2

-- test2.lua:
function read_file_numbers(filename)
  local array = {}
  local f = assert(
  while true do
    local v = f:read("*number")
    if not v then break end
    table.insert(array, v)
  return array

function write_file_numbers(filename, array)
  local f = assert(, 'w'))
  for _,v in ipairs(array) do
    f:write(v, ' ')

-- Create array in memory
local t = {3,4,5,2,5,3e20,10,2,7,-0.3}

-- Write arry to file
write_file_numbers("test2.dat", t)

-- Read array back from file
local t2 = read_file_numbers("test2.dat")

-- check that array is unchanged
for i=1, #t do
  assert(t[i] == t2[i])

Benchmarking Time

os.time() returns the integer number of seconds since 1970. This can be used for benchmarking:

local t0 = os.time()
local dt = os.time() - t0

os.clock() can provide more accuracy (thousandths of a second precision), but beware that it represents CPU time, which is the amount of time that your process has been running on the CPU. If your process is temporarily paused by the OS to allow other processes to run on the CPU or to wait for something like disk I/O, then this value will not increment during that time.

Printing numbers with a given decimal precision

This example provides a function log that operates like print but prints with five decimal places of precision.

local function log(...)
  local t = {...}
  for k,v in ipairs(t) do
    t[k] = ("%0.5f"):format(v)

log(math.pi, math.pi*2)
Any comments on this web page? (will be sent to SIS SIMION Support)
[Optional] Your name: email: phone/fax: