Lua#1 Lua Scripting for using with FEMM in Application to motor design

 


    Lua and FEMM Introduction

    In order to better understand the motor designing one has to understand the magnetic properties of the motor. FEMM is a software available for free and can be downloaded from the link given below, is used here. FEMM gives native support to the Lua scripting, and hence in this post, i mainly be focusing on the Lua scripting and magnetic simulation in FEMM.

    Remember, I am only discussing Lua for magnetic analysis but this software is not limited to the magnetics only. You can use it for Electrostatics, Heat transfer etc.

    In order to better understand the Lua, I have taken two example codes and discussed each function in detail, and finally, In this post i have taken different magnetic configuration and and plotted their magnetic field strength vs distance from the magnet.
      
    I would recommend have a look on the document at this link for more detail about each command. This document is dedicated to Lua for the use with OCTAVE but there is no problem in using the same commands with the FEMM. Because, in the case where OCTAVE is used with Lua, OCTAVE works as an IDE to write the Lua script for FEMM.

    YouTube video link for studying more about Lua.

    Example-1

    -- Useful functions
    function average(a, b)
      return (a + b) / 2
    end
    -- is used for commenting a line
    function get_x_y(radius, angle)
      x = radius * cos(angle * (pi / 180))
      y = radius * sin(angle * (pi / 180))
      return x, y
    end
    function create_magnets(r_inner, r_outer, a_start)
      r_middle = average(r_inner, r_outer)
      n_magnet_segments = (n_teeth / 2) - 1
      a_magnet_segment = (a_magnet * 2) / n_magnet_segments
      xi, yi = get_x_y(r_inner, a_start)
      xo, yo = get_x_y(r_outer, a_start)
      -- mi_addnode(x,y) are used to place node
      mi_addnode(xi, yi)
      mi_addnode(xo, yo)
      -- mi_addsegment(x1,y1,x2,y2) Add a new line segment from node closest to (x1,y1) to node closest to (x2,y2)
      mi_addsegment(xi, yi, xo, yo)
      -- mi_selectsegment(x,y) Select the line segment closest to (x,y)
      mi_selectsegment(get_x_y(r_middle, a_start))
      -- mi_copyrotate(bx, by, angle, copies )
          bx, by – base point for rotation
          angle – angle by which the selected objects are incrementally shifted to make each copy. angle is measured in degrees.
          copies – number of copies to be produced from the selected objects.
      mi_copyrotate(0, 0, a_magnet_segment, n_magnet_segments, 1)
      a_end = 180 - (90 - a_start)
      for m = 0, n_magnet_segments - 1, 1 do 
        angle = a_start + m * a_magnet_segment + a_magnet_segment / 2 
        x, y = get_x_y(r_middle, angle)
        -- mi_addblocklabel(x,y) Add a new block label at (x,y)
        mi_addblocklabel(x, y)
        -- mi_selectlabel(x,y) Select the label closest to (x,y). Returns the coordinates of the selected label.
        mi_selectlabel(x, y)
        if angle < 180 then
            -- mi_setblockprop(’blockname’, automesh, meshsize, ’incircuit’, magdir, group,turns) Set the selected block labels to have the properties:
              --Block property ’blockname’.
              --automesh:0 = mesher defers to mesh size constraint defined in meshsize,1 = mesher automatically chooses the mesh density.
              --meshsize: size constraint on the mesh in the block marked by this label.
              --Block is a member of the circuit named ’incircuit’.
              --The magnetization is directed along an angle in measured in degrees denoted by the parameter magdir
              --A member of group number group.
              --The number of turns associated with this label is denoted by turns.
          mi_setblockprop("NdFeB 40 MGOe",1,1,0,angle,0,0)
        else
          mi_setblockprop("NdFeB 40 MGOe",1,1,0,angle - 180,0,0)
        end
        mi_clearselected()
      end
      -- Make annoying wee bits air, deleting them blows things up
      for r = -r_middle, r_middle, 2 * r_middle domi_addblocklabel(r, 0)
        mi_selectlabel(r, 0) 
        mi_setblockprop("Air", 1, 1, 0, 0, 0, 0) 
      end
    end
    
    --Define the sizes of various things
    r_shaft = 10
    h_rotor = 25
    h_magnet = 6
    h_airgap = 2
    h_teeth = 32
    h_stator = 25
    a_magnet = 89 
    
    -- Degrees
    n_teeth = 12
    slot_ratio = 1 - 0.423 -- Teeth to slot ratio (< 1.0)
    
    -- Calculated values
    r_rotor = r_shaft + h_rotor
    r_magnet = r_rotor + h_magnet
    r_airgap = r_magnet + h_airgap
    r_teeth = r_airgap + h_teeth
    r_stator = r_teeth + h_stator
    
    newdocument(0)
    
    --NOTE: newdocument(doctype) Creates a new preprocessor document and opens up a new 
    --preprocessor window. Specify doctype to be 0 for a magnetics problem, 1 for an 
    --electrostatics problem, 2 for a heat flow problem, or 3 for a current flow 
    --problem. Alternative syntax for this function is create(doctype)
    
    mi_probdef(0, "millimeters", "planar", 1e-008, 300,  30, "succ.approx")
    
    --Draw start node
    mi_addnode(0,  0)
    
    -- rotor shaft
    mi_addnode(0, r_shaft)
    mi_addnode(0, -r_shaft)
    
    mi_addarc(0,r_shaft, 0,-r_shaft,180,1) 
    mi_addarc(0,-r_shaft, 0,r_shaft,180,1)
    
    -- rotor
    mi_addnode(0, r_rotor)
    mi_addnode(0, -r_rotor)
    
    mi_addarc(0,r_rotor, 0,-r_rotor,180,1) 
    mi_addarc(0,-r_rotor, 0,r_rotor,180,1)
    
    -- magnets
    mi_addnode(0, r_magnet)
    mi_addnode(0, -r_magnet)
    
    mi_addarc(0,r_magnet, 0,-r_magnet,180,1) 
    mi_addarc(0,-r_magnet, 0,r_magnet,180,1)
    
    -- airgap
    mi_addnode(0, r_airgap)
    mi_addnode(0, -r_airgap)
    
    mi_addarc(0,r_airgap, 0,-r_airgap,180,1) 
    mi_addarc(0,-r_airgap, 0,r_airgap,180,1)
    
    -- teeth
    mi_addnode(0, r_teeth)
    mi_addnode(0, -r_teeth)
    
    mi_addarc(0,r_teeth, 0,-r_teeth,180,1) 
    mi_addarc(0,-r_teeth, 0,r_teeth,180,1)
    
    -- stator yoke
    mi_addnode(0, r_stator)
    mi_addnode(0, -r_stator)
    
    mi_addarc(0,r_stator, 0,-r_stator,180,1) 
    mi_addarc(0,-r_stator, 0,r_stator,180,1) 
    
    --Get some materials
    mi_getmaterial("Air")      
    mi_getmaterial("M-36 Steel")
    mi_getmaterial("NdFeB 40 MGOe")
    
    -- modify magnet coercivity
    mi_modifymaterial("NdFeB 40 MGOe",3,962508)
    
    -- modify steel permeability
    mi_modifymaterial("M-36 Steel",1,16300)
    mi_modifymaterial("M-36 Steel",2,16300) 
    
    -- Draw magnets
    create_magnets(r_rotor, r_magnet, 90 - a_magnet)
    create_magnets(r_rotor, r_magnet, 180 + (90 - a_magnet))
    
    -- Draw teeth
    a_slot = (360 * slot_ratio) / n_teeth
    a_tooth = (360 * (1 - slot_ratio)) / n_teeth
    r_inner = r_airgap
    r_middle = average(r_airgap, r_teeth)
    r_outer = r_teeth
    
    -- Teeth
    mi_addnode(r_inner, 0)
    mi_addnode(r_outer, 0)
    mi_add_segment(r_inner, 0, r_outer, 0)
    mi_selectsegment(r_middle, 0)
    mi_copyrotate(0, 0, (a_slot + a_tooth), n_teeth, 1)
    
    -- Slots
    mi_selectsegment(r_middle, 0)
    mi_copyrotate(0, 0, a_slot, 1, 1)
    mi_selectsegment(get_x_y(r_middle, a_slot))
    mi_copyrotate(0, 0, (a_slot + a_tooth), n_teeth, 1)
    
    -- Select inner segments
    for angle = a_tooth / 2, 360, (a_tooth + a_slot) do
      mi_selectarcsegment(get_x_y(r_inner, angle))
    end
    
    -- Select outer segments
    for angle = a_slot + (a_tooth / 2), 360, (a_tooth + a_slot) do
      mi_selectarcsegment(get_x_y(r_outer, angle))
    end
    
    -- Delete them
    mi_deleteselectedarcsegments()
    
    --Define air and steel regions
    
    -- Shaft as air
    r_middle = average(0, r_shaft)
    mi_addblocklabel(0,r_middle)
    mi_selectlabel(0,r_middle)
    mi_setblockprop("Air",  1,  1,  0,  0,  0,  0) 
    
    --airgap
    r_middle = average(r_magnet, r_airgap)
    mi_clearselected()
    mi_addblocklabel(0,r_middle)
    mi_selectlabel(0,r_middle)
    mi_setblockprop("Air",  1,  1,  0,  0,  0,  0)
    
    -- Rotor Steel
    r_middle = average(r_shaft, r_rotor)
    mi_clearselected()
    mi_addblocklabel(0,r_middle)
    mi_selectlabel(0,r_middle)
    mi_setblockprop("M-36 Steel",  1,  1,  0,  0,  0,  0)
    
    -- Stator Steel
    r_middle = average(r_teeth, r_stator)
    mi_clearselected()
    mi_addblocklabel(0,r_middle)
    mi_selectlabel(0,r_middle)
    mi_setblockprop("M-36 Steel",  1,  1,  0,  0,  0,  0)
    
    -- around stator
    r_outside = r_stator + 10
    mi_clearselected()
    mi_addblocklabel(0,r_outside)
    mi_selectlabel(0,r_outside)
    mi_setblockprop("Air",  1,  1,  0,  0,  0,  0)
    
    mi_clearselected()
    
    --Draw a square around the model
    r_outside = r_stator + 20
    mi_addnode(r_outside, r_outside)
    mi_addnode(-r_outside, r_outside)
    mi_addnode(r_outside,-r_outside)
    mi_addnode(-r_outside,-r_outside)
    mi_addsegment(r_outside,r_outside,-r_outside,r_outside)
    mi_addsegment(-r_outside,r_outside,-r_outside,-r_outside)
    mi_addsegment(-r_outside,-r_outside,r_outside,-r_outside)
    mi_addsegment(r_outside,-r_outside,r_outside,r_outside)
    
    mi_addboundprop("A1",0,0,0,0,0,0,0,0,0)
    mi_selectsegment(r_outside,r_outside)
    mi_selectsegment(-r_outside,r_outside)
    mi_selectsegment(-r_outside,-r_outside)
    mi_selectsegment(r_outside,-r_outside)
    mi_setsegmentprop("A1", 0,1,0,0)
    
    mi_zoomnatural()
    
    mi_saveas("model.fem")
    mi_createmesh()
    mi_analyze(0)
    mi_loadsolution()
    
    -- show flux density plot
    mo_showdensityplot(1,0,1.5,0,"bmag")
    
    -- calculate flux per pole and main airgap flux density and write results to text file
    -- Draw a contour at the midpoint of the airgap spanning the width of the magnet
    -- calculate line integral
    mo_clearcontour()
    mo_seteditmode("contour")
    
    r_middle = average(r_magnet, r_airgap)
    mo_addcontour(get_x_y(r_middle, 90 - a_magnet))
    mo_addcontour(get_x_y(r_middle, 180 - (90 - a_magnet)))
    mo_bendcontour(a_magnet * 2,1)
    phi,Bn = mo_lineintegral(0)
    handle=openfile("Results.txt","w")
    write(handle,"Flux per Pole:\n\n",phi,"\n\n","Mean Airgap Flux Density:\n\n",Bn,"\n")
    closefile(handle)
    
    --Draw a contour at the midpoint of the airgap covering all 2 poles
    r_middle = average(r_magnet, r_airgap)
    mo_clearcontour()
    mo_seteditmode("contour")
    mo_addcontour(0,r_middle)
    mo_addcontour(0,-r_middle)
    mo_bendcontour(180,1)
    mo_addcontour(0,r_middle)
    mo_bendcontour(180,1)
    
    mo_makeplot(2,360)

    Example:2

    function y=inductance(n,ri,ro,z)
    	openfemm;
    	newdocument(0);
    	mi_probdef(0,'inches','axi',1e-8,0,30);
    	mi_drawrectangle(ri,-z/2,ro,z/2);
    	r=2*max([ro,ri,z]);
    	mi_drawarc(0,-r,0,r,180,5);
    	mi_drawline(0,-r,0,r);
    	mi_addcircprop('icoil',1,1);
    	mi_addblocklabel((ri+ro)/2,0);
    	mi_addblocklabel(0.75*r,0);
    	mi_addmaterial('coil',1,1,0,0,0,0,0,1,0,0,0);
    	mi_addmaterial('air' ,1,1,0,0,0,0,0,1,0,0,0);
    	mi_addboundprop('abc',0,0,0,0,0,0,1/(r*0.0254*pi*4.e-7),0,2);
    	mi_selectlabel((ri+ro)/2,0);
    	mi_setblockprop('coil',0,r/20,'icoil',0,0,n);
    	mi_clearselected;
    	mi_selectlabel(0.75*r,0);
    	mi_setblockprop('air',0,r/100,'',0,0,0);
    	mi_clearselected;
    	mi_selectarcsegment(r,0);
    	mi_setarcsegmentprop(5,'abc',0,0);
    	mi_saveas('c:\\femm42\\examples\\tmp.fem');
    	mi_analyze;
    	mi_loadsolution;
    	c=mo_getcircuitproperties('icoil');
    	y=c(3);
    	closefemm;
    end

    Lua code for magnetic field intensity Analysis

    In this example, two N52 magnet of different shapes 5mmx5mm and 5mmx2.5mm are taken, and are placed in environment filled with air, to study the magnetic field distribution near the magnet surface. They are arranged in such a way that they have same magnetic filed orientation as shown in figure below.

    Figure shows magnet placement and orientation information after mesh solution in FEMM

    Code

    newdocument(0);
    mi_probdef(0,'millimeters','planar',1e-8,0,30);
    mi_addmaterial('air' ,1,1,0,0,0,0,0,1,0,0,0);
    mi_addmaterial("N42", 1.05, 1.05, 943000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
    magnet="N42";
    --mi_addboundprop('abc',0,0,0,0,0,0,1/(r*0.0254*pi*4.e-7),0,2);
    
    squareMagnet=5;
    mi_addnode(0,0);
    mi_addnode(0,squareMagnet);
    mi_addnode(squareMagnet,0);
    mi_addnode(squareMagnet,squareMagnet);
    
    mi_addsegment(0,0,0,squareMagnet);
    mi_addsegment(0,squareMagnet,squareMagnet,squareMagnet);
    mi_addsegment(squareMagnet,squareMagnet,squareMagnet,0);
    mi_addsegment(squareMagnet,0,0,0);
    mi_addblocklabel(squareMagnet/2,squareMagnet/2);
    
    
    w=5;
    h=2.5;
    x0=40;
    y0=0;
    mi_addnode(x0,y0);
    mi_addnode(x0+w,y0);
    mi_addnode(x0+w,y0+h);
    mi_addnode(x0,y0+h);
    
    mi_addsegment(x0,y0,x0+w,y0);
    mi_addsegment(x0+w,y0,x0+w,y0+h);
    mi_addsegment(x0+w,y0+h,x0,y0+h);
    mi_addsegment(x0,y0+h,x0,y0);
    mi_addblocklabel(x0+w/2,y0+h/2);
    
    
    
    gap=20;
    airH=gap+h+gap;
    airW=gap+x0+w+gap;
    airx0=-gap;
    airy0=-gap;
    mi_addnode(airx0,airy0);
    mi_addnode(airx0+airW,airy0);
    mi_addnode(airx0+airW,airy0+airH);
    mi_addnode(airx0,airy0+airH);
    
    mi_addsegment(airx0,airy0,airx0+airW,airy0);
    mi_addsegment(airx0+airW,airy0,airx0+airW,airy0+airH);
    mi_addsegment(airx0+airW,airy0+airH,airx0,airy0+airH);
    mi_addsegment(airx0,airy0+airH,airx0,airy0);
    mi_addblocklabel(airx0+5,airy0+5);
    
    
    mi_selectlabel(airx0+5,airy0+5); -- Air
    mi_setblockprop("air",0,0,0,0,0,0);
    mi_clearselected();
    
    mi_getmaterial(magnet);
    
    mi_selectlabel(x0+w/2,y0+h/2); --Magnets 1
    mi_setblockprop(magnet,0,0,0,90,0,0);
    mi_clearselected();
    
    mi_selectlabel(squareMagnet/2,squareMagnet/2); --Magnets 2
    mi_setblockprop(magnet,0,0,0,90,0,0);
    mi_clearselected();
    
    
    
    --adding boundary prop
    mi_addboundprop('airbound', 0, 0, 0, 0, 0, 0, 0, 0, 0);
    
    mi_selectsegment(airx0,airy0+airH/2) --left side
    mi_setsegmentprop('airbound',0,1,0,0)
    mi_clearselected()
    
    mi_selectsegment(airx0+airW,airH/2) --righ side
    mi_setsegmentprop('airbound',0,1,0,0)
    mi_clearselected()
    
    mi_selectsegment(airx0+airW/2,airy0+airH) --top side
    mi_setsegmentprop('airbound',0,1,0,0)
    mi_clearselected()
    
    mi_selectsegment(airx0+airW/2,airy0) --bottom side
    mi_setsegmentprop('airbound',0,1,0,0)
    mi_clearselected()
    
    
    mi_saveas('D:\\Desktop from D drive\\Motor Study using lua\\sizeVsfieldstrength.fem');

    OUTPUT

    Case-1: Horizontal red line only

    Fig.1 Showing magnetic field line produced by magnet, and a horizontal line is drawn near the magnet north pole the measure the magnetic field along the line.

    In Fig.2 it evident that as we move from left to right on the horizontal red line(Fig.1), the magnetic field increases till the first magnet of 5mmx5mm. when the magnetic field is measured in front of the magnet at 1mm of distance from the magnet then magnetic field decreases a bit and and then rises again. As we farther move from the magnet, magnetic field starts decreasing. Same happes to the smaller sized magnet.
    Fig.2 Magnetic field strength measured along the red line (measured while moving from left to right on red line). 

    Case-2: Horizontal and vertical red lines

    Fig.1 Shows the magnetic field line and reference red line

    Fig.2 Shows the magnetic field intensity vs distance. Measured while moving on red line

    Case-3: Vertical red lines

    Fig.1

    Fig.2

    Rotating smaller magnet

    In this example, only the smaller magnet is rotated by 90 degrees anticlockwise by maintaining it's magnetic orientation.
    newdocument(0);
    mi_probdef(0,'millimeters','planar',1e-8,0,30);
    mi_addmaterial('air' ,1,1,0,0,0,0,0,1,0,0,0);
    mi_addmaterial("N42", 1.05, 1.05, 943000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
    magnet="N42";
    --mi_addboundprop('abc',0,0,0,0,0,0,1/(r*0.0254*pi*4.e-7),0,2);
    
    squareMagnet=7.4;
    mi_addnode(0,0);
    mi_addnode(0,squareMagnet);
    mi_addnode(squareMagnet,0);
    mi_addnode(squareMagnet,squareMagnet);
    
    mi_addsegment(0,0,0,squareMagnet);
    mi_addsegment(0,squareMagnet,squareMagnet,squareMagnet);
    mi_addsegment(squareMagnet,squareMagnet,squareMagnet,0);
    mi_addsegment(squareMagnet,0,0,0);
    mi_addblocklabel(squareMagnet/2,squareMagnet/2);
    
    w=7.4;
    h=1.5;
    x0=40;
    y0=0;
    mi_addnode(x0,y0);
    mi_addnode(x0+w,y0);
    mi_addnode(x0+w,y0+h);
    mi_addnode(x0,y0+h);
    
    mi_addsegment(x0,y0,x0+w,y0);
    mi_addsegment(x0+w,y0,x0+w,y0+h);
    mi_addsegment(x0+w,y0+h,x0,y0+h);
    mi_addsegment(x0,y0+h,x0,y0);
    mi_addblocklabel(x0+w/2,y0+h/2);
    
    gap=20;
    airH=gap+h+gap;
    airW=gap+x0+w+gap;
    airx0=-gap;
    airy0=-gap;
    mi_addnode(airx0,airy0);
    mi_addnode(airx0+airW,airy0);
    mi_addnode(airx0+airW,airy0+airH);
    mi_addnode(airx0,airy0+airH);
    
    mi_addsegment(airx0,airy0,airx0+airW,airy0);
    mi_addsegment(airx0+airW,airy0,airx0+airW,airy0+airH);
    mi_addsegment(airx0+airW,airy0+airH,airx0,airy0+airH);
    mi_addsegment(airx0,airy0+airH,airx0,airy0);
    mi_addblocklabel(airx0+5,airy0+5);
    
    mi_selectlabel(airx0+5,airy0+5); -- Air
    mi_setblockprop("air",0,0,0,0,0,0);
    mi_clearselected();
    
    mi_getmaterial(magnet);
    
    mi_selectlabel(x0+w/2,y0+h/2); --Magnets 1
    mi_setblockprop(magnet,0,0,0,90,0,0);
    mi_clearselected();
    
    mi_selectlabel(squareMagnet/2,squareMagnet/2); --Magnets 2
    mi_setblockprop(magnet,0,0,0,90,0,0);
    mi_clearselected();
    
    --adding boundary prop
    mi_addboundprop('airbound', 0, 0, 0, 0, 0, 0, 0, 0, 0);
    
    mi_selectsegment(airx0,airy0+airH/2) --left side
    mi_setsegmentprop('airbound',0,1,0,0)
    mi_clearselected()
    
    mi_selectsegment(airx0+airW,airH/2) --righ side
    mi_setsegmentprop('airbound',0,1,0,0)
    mi_clearselected()
    
    mi_selectsegment(airx0+airW/2,airy0+airH) --top side
    mi_setsegmentprop('airbound',0,1,0,0)
    mi_clearselected()
    
    mi_selectsegment(airx0+airW/2,airy0) --bottom side
    mi_setsegmentprop('airbound',0,1,0,0)
    mi_clearselected()
    
    mi_saveas('D:\\Desktop from D drive\\Motor Study using lua\\sizeVsfieldstrength.fem');

    OUTPUT

    Fig.1 Magnetic field line
    Fig.2: Magnetic field line in grey color
    Fig.3 Magnetic field line and reference red line
    Fig.4 Magnetic field intensity vs distance, measured along horizontal red line shown in Fig.3

    Fig.5: Magnetic field intensity vs distance, measured along vertical red lines

    Fig.6 Magnetic field lines and reference red lines
    Fig.7: Magnetic field intensity vs distance, measured along red lines shown in Fig.6

    Changing the magnet arrangement

    In this example, a new arrangement of three N42 magnet is placed in the air and their north pole is facing upward. Magnetic field intensity is analyzed and shown in figures below.

    Figure showing magnet arrangement and it's surrounding environment
    newdocument(0);
    mi_probdef(0,'millimeters','planar',1e-8,0,30);
    mi_addmaterial('air' ,1,1,0,0,0,0,0,1,0,0,0);
    mi_addmaterial("N42", 1.05, 1.05, 943000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
    magnet="N42";
    --mi_addboundprop('abc',0,0,0,0,0,0,1/(r*0.0254*pi*4.e-7),0,2);
    
    
    --block A
    Wa=7.4;
    Ha=7.4;
    Xa=0;
    Ya=0;
    mi_addnode(Xa,Ya);
    mi_addnode(Xa,Ha+Ya);
    mi_addnode(Wa+Xa,Ya);
    mi_addnode(Wa+Xa,Ha+Ya);
    
    mi_addsegment(Xa,Ya,Xa,Ha+Ya);
    mi_addsegment(Xa,Ha+Ya,Wa+Xa,Ha+Ya);
    mi_addsegment(Wa+Xa,Ha+Ya,Wa+Xa,Ya);
    mi_addsegment(Wa+Xa,Ya,Xa,Ya);
    mi_addblocklabel(Xa+Wa/2,Ya+Ha/2);
    
    
    --block B
    Wb=7.4;
    Hb=7.4;
    Xb=Wa+1;
    Yb=0;
    mi_addnode(Xb,Yb);
    mi_addnode(Xb,Hb+Yb);
    mi_addnode(Wb+Xb,Yb);
    mi_addnode(Wb+Xb,Hb+Yb);
    
    mi_addsegment(Xb,Yb,Xb,Hb+Yb);
    mi_addsegment(Xb,Hb+Yb,Wb+Xb,Hb+Yb);
    mi_addsegment(Wb+Xb,Hb+Yb,Wb+Xb,Yb);
    mi_addsegment(Wb+Xb,Yb,Xb,Yb);
    mi_addblocklabel(Xb+Wb/2,Yb+Hb/2);
    
    --block C
    Wc=15;
    Hc=3;
    Xc=40;
    Yc=0;
    mi_addnode(Xc,Yc);
    mi_addnode(Xc,Hc+Yc);
    mi_addnode(Wc+Xc,Yc);
    mi_addnode(Wc+Xc,Hc+Yc);
    
    mi_addsegment(Xc,Yc,Xc,Hc+Yc);
    mi_addsegment(Xc,Hc+Yc,Wc+Xc,Hc+Yc);
    mi_addsegment(Wc+Xc,Hc+Yc,Wc+Xc,Yc);
    mi_addsegment(Wc+Xc,Yc,Xc,Yc);
    mi_addblocklabel(Xc+Wc/2,Yc+Hc/2);
    
    
    
    
    gap=20;
    airH=gap+Hc+gap;
    airW=gap+Xc+Wc+gap;
    airx0=-gap;
    airy0=-gap;
    mi_addnode(airx0,airy0);
    mi_addnode(airx0+airW,airy0);
    mi_addnode(airx0+airW,airy0+airH);
    mi_addnode(airx0,airy0+airH);
    
    mi_addsegment(airx0,airy0,airx0+airW,airy0);
    mi_addsegment(airx0+airW,airy0,airx0+airW,airy0+airH);
    mi_addsegment(airx0+airW,airy0+airH,airx0,airy0+airH);
    mi_addsegment(airx0,airy0+airH,airx0,airy0);
    mi_addblocklabel(airx0+5,airy0+5);
    
    
    mi_selectlabel(airx0+5,airy0+5); -- Air
    mi_setblockprop("air",0,0,0,0,0,0);
    mi_clearselected();
    
    mi_getmaterial(magnet);
    
    mi_selectlabel(Xa+Wa/2,Yb+Hb/2); --Magnets for block A
    mi_setblockprop(magnet,0,0,0,90,0,0);
    mi_clearselected();
    
    mi_selectlabel(Xb+Wb/2,Yb+Hb/2); --Magnets for block B
    mi_setblockprop(magnet,0,0,0,90,0,0);
    mi_clearselected();
    
    mi_selectlabel(Xc+Wc/2,Yc+Hc/2); --Magnet for block C
    mi_setblockprop(magnet,0,0,0,90,0,0);
    mi_clearselected();
    
    
    
    --adding boundary prop
    mi_addboundprop('airbound', 0, 0, 0, 0, 0, 0, 0, 0, 0);
    
    mi_selectsegment(airx0,airy0+airH/2) --left side
    mi_setsegmentprop('airbound',0,1,0,0)
    mi_clearselected()
    
    mi_selectsegment(airx0+airW,airH/2) --righ side
    mi_setsegmentprop('airbound',0,1,0,0)
    mi_clearselected()
    
    mi_selectsegment(airx0+airW/2,airy0+airH) --top side
    mi_setsegmentprop('airbound',0,1,0,0)
    mi_clearselected()
    
    mi_selectsegment(airx0+airW/2,airy0) --bottom side
    mi_setsegmentprop('airbound',0,1,0,0)
    mi_clearselected()
    
    
    mi_saveas('D:\\Desktop from D drive\\Motor Study using lua\\sizeVsfieldstrength.fem');

    OUTPUT

    Fig.1 Shows magnetic field lines produced by this arrangement and reference red line

    Fig.2 Magnetic field intensity is plotted along the red line.

    Changing the magnet size

    newdocument(0);
    mi_probdef(0,'millimeters','planar',1e-8,0,30);
    mi_addmaterial('air' ,1,1,0,0,0,0,0,1,0,0,0);
    mi_addmaterial("N42", 1.05, 1.05, 943000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
    magnet="N42";
    --mi_addboundprop('abc',0,0,0,0,0,0,1/(r*0.0254*pi*4.e-7),0,2);
    
    squareMagnet=15;
    mi_addnode(0,0);
    mi_addnode(0,squareMagnet);
    mi_addnode(squareMagnet,0);
    mi_addnode(squareMagnet,squareMagnet);
    
    mi_addsegment(0,0,0,squareMagnet);
    mi_addsegment(0,squareMagnet,squareMagnet,squareMagnet);
    mi_addsegment(squareMagnet,squareMagnet,squareMagnet,0);
    mi_addsegment(squareMagnet,0,0,0);
    mi_addblocklabel(squareMagnet/2,squareMagnet/2);
    
    
    w=15;
    h=3;
    x0=40;
    y0=0;
    mi_addnode(x0,y0);
    mi_addnode(x0+w,y0);
    mi_addnode(x0+w,y0+h);
    mi_addnode(x0,y0+h);
    
    mi_addsegment(x0,y0,x0+w,y0);
    mi_addsegment(x0+w,y0,x0+w,y0+h);
    mi_addsegment(x0+w,y0+h,x0,y0+h);
    mi_addsegment(x0,y0+h,x0,y0);
    mi_addblocklabel(x0+w/2,y0+h/2);
    
    
    
    gap=20;
    airH=gap+h+gap;
    airW=gap+x0+w+gap;
    airx0=-gap;
    airy0=-gap;
    mi_addnode(airx0,airy0);
    mi_addnode(airx0+airW,airy0);
    mi_addnode(airx0+airW,airy0+airH);
    mi_addnode(airx0,airy0+airH);
    
    mi_addsegment(airx0,airy0,airx0+airW,airy0);
    mi_addsegment(airx0+airW,airy0,airx0+airW,airy0+airH);
    mi_addsegment(airx0+airW,airy0+airH,airx0,airy0+airH);
    mi_addsegment(airx0,airy0+airH,airx0,airy0);
    mi_addblocklabel(airx0+5,airy0+5);
    
    
    mi_selectlabel(airx0+5,airy0+5); -- Air
    mi_setblockprop("air",0,0,0,0,0,0);
    mi_clearselected();
    
    mi_getmaterial(magnet);
    
    mi_selectlabel(x0+w/2,y0+h/2); --Magnets 1
    mi_setblockprop(magnet,0,0,0,90,0,0);
    mi_clearselected();
    
    mi_selectlabel(squareMagnet/2,squareMagnet/2); --Magnets 2
    mi_setblockprop(magnet,0,0,0,90,0,0);
    mi_clearselected();
    
    
    
    --adding boundary prop
    mi_addboundprop('airbound', 0, 0, 0, 0, 0, 0, 0, 0, 0);
    
    mi_selectsegment(airx0,airy0+airH/2) --left side
    mi_setsegmentprop('airbound',0,1,0,0)
    mi_clearselected()
    
    mi_selectsegment(airx0+airW,airH/2) --righ side
    mi_setsegmentprop('airbound',0,1,0,0)
    mi_clearselected()
    
    mi_selectsegment(airx0+airW/2,airy0+airH) --top side
    mi_setsegmentprop('airbound',0,1,0,0)
    mi_clearselected()
    
    mi_selectsegment(airx0+airW/2,airy0) --bottom side
    mi_setsegmentprop('airbound',0,1,0,0)
    mi_clearselected()
    
    
    mi_saveas('D:\\Desktop from D drive\\Motor Study using lua\\sizeVsfieldstrength.fem');

    OUTPUT




    OUTPUT:2








    Comments