====== 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 {{page>insert/paypal}} ===== 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 ===== Automatic limits in colour maps ===== Full command executed with just the legend parameter set to -1 shows default limits for the legend. This seems to work in pure LUA, and mo_showdensityplot(-1) does not work even in pure LUA: mo_showdensityplot(legend,gscale,upper_B,lower_B,type) mo_showdensityplot(-1,0,0,1,"bmag") legend: Set to 0 to hide the plot legend or 1 to show the plot legend. (note that: ''mo_showdensityplot(-1)'' will not work). ===== Rounding in LUA ===== Rounding does not appear to be immediately available in LUA 4.0. Teh function round_LUA(var_input, round_precision) var_floor = floor(var_input/round_precision)*round_precision var_ceil = ceil(var_input/round_precision)*round_precision var_rounded = var_floor if (var_input > (var_floor + 5*round_precision/10) ) then var_rounded = var_ceil end return(var_rounded) end -- end function var_input = 123.44900000003 -- input variable to be rounded round_precision = 10 -- rounding precision, use: 100, 10, 1, 0.1, 0.01 etc. var_output = round_LUA(var_input, round_precision) print(var_input) print(var_output)