User Tools

Site Tools


lua_scripting_tips

LUA scripting tips

This page contains tips for using Lua 4.0 with FEMM 4.2. Some of these are documented in FEMM help file (User's manual) but some are not.

Full LUA 4.0 documentation: http://www.lua.org/manual/4.0/

Comments

To insert a comment use two hyphens, at the beginning of a line or at the end of command:

-- this is a comment
x = x + 1;   -- this is also a comment 

FOR loop and IF statement

The relational operators in Lua are

equal, not equal, smaller than, greater than, equal or smaller, equal or greater
  ==      ~=           <             >           <=                >=

Example of a FOR loop and an IF statement

for n=1,20 do
    -- Comment within FOR loop
    hi_analyze();
    hi_loadsolution();
    ho_savebitmap("T"..n..".bmp");
    ho_close();
 
        if (n<15) then
            mi_selectgroup(1)
            mi_movetranslate(0,0.1)
        end -- end of IF
 
end  -- end of FOR loop

There is no need to pre-declare the “counting” variable (“n” in the example above). It gets declared at the point of use directly.

The functions of AND and OR

        if (n<15 and x>0) then
        end
        
        if (n<15 or x>0) then
        end

Multiple output values of function

Some functions return multiple values.

Example: To catch all values at a point with coordinates (0.01,0) use:

A, B1, B2, Sig, E, H1, H2, Je, Js, Mu1, Mu2, Pe, Ph = mo_getpointvalues(0.01,0)

where the names before the = sign are all the variables to which the data will be transferred. If you are interested only in H1 and H2, use a dummy variables, which can be discarded, and leave others out, for example:

x1, x2, x3, x4, x5, H1, H2 = mo_getpointvalues(0.01,0)

To the exact format refer to the User's Manual in FEMM. The definitions of functions can change in newer updates.

  • mo_getpointvalues(X,Y) Get the values associated with the point at x,y RETURN values in order
Symbol Definition
A vector potential A or flux φ
B1 flux density Bx if planar, Br if axisymmetric
B2 flux density By if planar, Bz if axisymmetric
Sig electrical conductivity σ
E stored energy density
H1 field intensity Hx if planar, Hr if axisymmetric
H2 field intensity Hy if planar, Hz if axisymmetric
Je eddy current density
Js source current density
Mu1 relative permeability μx if planar, μr if axisymmetric
Mu2 relative permeability μy if planar, μz if axisymmetric
Pe Power density dissipated through ohmic losses
Ph Power density dissipated by hysteresis

Constants in FEMM

FEMM has certain constants defined, which can be used as inputs and will be evaluated correctly.

  • PI = Pi = pi = 3.14159 - mathematical constant π
  • uo = PI*4.e-7 - magnetic permeability of free space (lower case “u” and “o”, not zero)
  • eo = 8.85418781762e-12 - electrical permittivity of free space (lower case “e” and “o”, not zero)

These are defined in the text file (use Notepad to open):

C:\femm42\bin\init.lua (or depending where FEMM was installed into).

Sequential file names

Sometimes results need to be written into files whose numbers increase in a sequence. This can be achieved by using the structure as in the example below, where: “string”..n..“string” allows including the variable “n” in the name:

for n=1,10 do  -- FOR loop with "n" as the numerator
 
   -- write results to a sequentially numbered file
   handle1 = openfile("filename_"..n..".txt","w")   -- w = rewrite, a = append, r = read      
   write(handle1, variable_A, "\n")              -- write "variable_A" to the file   
   closefile(handle1)
 
  end
 
pause() -- this can be used for debugging
Helpful page? Support Encyclopedia Magnetica.

All we need is $0.25 per month? Come on… ;-)

Complex numbers

Lua in FEMM handles complex numbers automatically, for example:

a = 1 + I*2
b = 0.3 - I*0.4
a + b = 1.3 + I*1.6
a * b = 1.1 + I*0.2
a/b = -2 + I*4
re(b) = 0.3
im(b) = -0.4
abs(b) = 0.5
lua_scripting_tips.txt · Last modified: 2024/07/24 23:09 by stanzurek