/out,pm_rotor,out
! *abbr,ames,stat
/com,    pm_rotor.mac
/com,   10/26/01   mesh divisions refined
!   Generates the PM rotor based on parameters
!    arg1 = mesh refinement, 1 is minimum number of elements, 5 is most 
!           most refinement
!    arg2 = 0  use lower order element for the iron
!         > 0  use higher order element for the iron
!    arg3 = number of rotor teeth to be generated
!         = 0 (default), no action, no additional teeth are generated
!         ne 0 Build arg3 number of teeth
!    arg4 = 0  use lower order element for the magnet and air gap
!         > 0  use higher order element for the magnet and air gap
!    arg5 = additional mesh control factor to control the elements at the 
!           back of the magnet and the inner radius of the return path
!           Defaults to 1
!
!
/com, 12/25/00  version
/com,
fini
/prep7
/uis,msgpop,3

_err=0
_error=0
 /nerr,1,1e6
*get,_mod_id,parm,rotor_id,type
*if,_mod_id,eq,3,then
 *if,rotor_id,ne,'pmrotor',then
 /out
  *msg,error,rotor_id
 The model type ( %c ) does not correspond to a permanent magnet rotor
 /out,pm_rotor,out,,append
 _error=1
 *endif
*else
 /out
 _error=1
 *msg,error
 The model type identifier specified is not a "character" type parameter&
 or it was not specified
 /out,pm_rotor,out,,append
*endif

*if,_error,eq,1,:err


!  geometry error check

_error=0

_nm_vn=3
*set,_nm_vv
*dim,_nm_vv,,_nm_vn
_nm_vv(1)=rr1,nrp,gap
*set,_nm_v
*dim,_nm_v,char,_nm_vn
_nm_v(1)='rr1','nrp','gap'
 /out
*do,_i,1,_nm_vn
*if,_nm_vv(_i),le,0,then
  /nerr,1,1e6
  *msg,error,_nm_v(_i)
  Parameter { %c }  is negative or zero.
  _error=_error+1
*endif
*enddo

/out,pm_rotor,out,,append

*if,loaf,eq,1,then
  m_flat=1
*endif

*get,_tmagtop,parm,magtop,type            !  9/16/99
*if,_tmagtop,ne,3,then                    !  9/16/99
 _magflat=0                               !  9/16/99
*else
  *if,magtop,eq,'flat',then               !  9/16/99
    /com, the top of the magnet is flat-no curvature
    _magflat=1                              !  9/16/99
    *if,magh,le,0,then
     /nerr
     /out
     *msg,error
     The top of the magnet is flat, but the magnet thickness&
      has not been specified-no action.
     /out,pm_rotor,out,,append
     _err=1
     /nerr,0,1e5
    *endif
  *endif
  esel,none
  *get,_ck_st,comp,stator,type
  _ec_s=0
  *if,_ck_st,gt,0,then
   cmsel,,stator
   *get,_ec_s,elem,,count
  *endif
  *if,_ec_s,gt,0,then
   /nerr
   /out
   *msg,error
    The option for the square magnet is being used.  For this option&
    the rotor must be constructed first, since the boundary between&
    the rotor and the stator models has to be readjusted.  Please clear the&
    database and construct the rotor first, then construct the stator.
   /out,pm_rotor,out,,append
   _error=1
   /nerr,0,1e5
  *endif
*endif                                    !  9/16/99
*if,_error,eq,1,:err





*if,(1-_magflat)*m_flat,eq,1,then   !  5/29/99
  *if,(rc1+rc2),eq,0,then
    /out
    *msg,error
    The flat bottom for the magnet option has been selected, but the&
    parameters {rc1} and {rc2} have not been specified.  The {magh} parameter&
    cannot be used with the flat bottom magnet option
    _error=_error+1
    /out,pm_rotor,out,,append
  *endif
*endif                  !  5/29/99
*if,rbc,le,0,then
 *if,rc1+rc2,le,0,then
   rbc=rr2+magh+gap/2   !  5/17/00
 *else
   rbc=rc1+rc2+gap/2    !  12/13/00
 *endif
*endif

*if,_magflat,eq,1,then

 *if,magh,ne,0,then

  ! rbc=rr2+magh+gap/2

 *elseif,rc1+rc2,ne,0,then
  rbc=rc1+rc2+gap/2
 *else
 _err=1
 *endif

 *if,magw,gt,0,then
  _y4=rr2*tan(magw/nrp)
 *else
  _y4=magl/2
 *endif
 /com, rbc is being reset
 _r4=sqrt((rr2+magh)**2+_y4**2)
 rbc=(_r4+r3)/2
 gap=r3-_r4
 *if,gap,le,0,then
  /nerr
  /out
  *msg,error
   For the top of magnet option, the edge of the magnet crosses over&
   into the stator. Check the magnet height and magnet width.
  /out,pm_rotor,out,,append
  _error=1
  /nerr,0,1e5
 *endif
*endif

*if,rc2+rc1,le,0,then
  *if,rr2*magh,le,0,then
   *msg,error
   Parameter {rr2} OR {magh} are negative or zero.
  _error=_error+1
  *endif
*endif
*if,magl+magw,le,0,then
  *msg,error
  Parameter {magl} and {magw} are negative or zero.
  _error=_error+1
*endif
*if,magw,gt,180,then
  *msg,error
  The magnet angle width cannot be greater than 180
  _error=_error+1
*endif
/nerr,0,1e6
*if,_error,ge,1,:err

*if,arg1,eq,0,then
 arg1=2
*endif
_cd1=1    !   radial divisons for the air space 
!             between two phases in the same slot
*get,_elmtyp,etyp,,num,max
*if,_elmtyp,eq,0,then
 _elmtyp=1
*endif
_elmtyp=_elmtyp+1     !    7/19/96
_elctyp=_elmtyp+1

*if,arg2,eq,0,then
  et,_elmtyp,13
*else
  et,_elmtyp,53
*endif

*if,arg4,eq,0,then
  et,_elctyp,13
*else
  et,_elctyp,53
*endif

*if,arg6,eq,1,then
  et,_elctyp,53,3
*endif

*if,arg5,eq,0,then
  arg5=1
*endif
f_2=arg5 !   additional factor for mesh at back of the magnet and the rotor iron

immed
esel,none
nsel,none
asel,none
ksel,none
lsel,none
cmsel,,rotor
cpdel,all,,,any
cedel,all,,,any
acle,all
adel,all,,,1
ldel,all,,,1
kdel,all
edel,all
ndel,all
!numc,kp
!numc,line
!numc,area
numc,elem
numc,node
numc,cp
numc,ce

alls
cm,_ckp,kp
cm,_cls,line
cm,_car,area
cmgrp,_cmp,_ckp,_cls,_car




!   determine the current maximum real constant set
esel,all
*get,_elmx,elem,,num,max
_rl_strt=0
*if,_elmx,ne,0,then
*set,_mskv
*dim,_mskv,,_elmx
*vget,_mskv(1),elem,1,esel
*set,_r_e
*dim,_r_e,,_elmx
*vmask,_mskv(1)
*vget,_r_e(1),elem,1,attr,real
*vmask,_mskv(1)
*vscfun,_rl_strt,max,_r_e(1)
_zz=100*nint(_rl_strt/100)
*if,_zz,lt,_rl_strt,then
  _rl_strt=_zz+51
*else
  _rl_strt=_zz
*endif
_zz=
*set,_mskv
*set,_r_e
esel,none
*endif

!  compute locations
_mxkp=11
*set,_kp_n_
*dim,_kp_n,,_mxkp
_60=180
*set,_x
*dim,_x,,_mxkp
*set,_y
*dim,_y,,_mxkp
*afun,deg
!*if,magh,ne,0,then    !  rem 9/23
! rbc=rr2+magh+gap/2
!*elseif,rc1+rc2,ne,0,then
! rbc=rc1+rc2+gap/2
!*else
!_err=1
!*endif
*if,nrp,eq,0,then
 _err=2
 nrp=1
*endif

_x(1)=rbc
_y(1)=0
_x(2)=rbc*cos(180/nrp)
_y(2)=rbc*sin(180/nrp)

*if,magh,ne,0,then
 _x(3)=rr2+magh
 _y(3)=0
*else
 _x(3)=rc1+rc2
 _y(3)=0
*endif


*if,(1-_magflat)*(rc1+rc2),ne,0,then    !  9/16/99
 ! the shaped pole option
 *if,magw,ne,0,then
  !  magnet width is by the angle 
  t_b=tan(magw/nrp)
  _x(4)=rc1+sqrt(rc1**2-(1+t_b**2)*(rc1**2-rc2**2))
  _x(4)=_x(4)/(1+t_b**2)
  _y(4)=_x(4)*t_b
 *elseif,magl,ne,0,then
  _x(4)=rc1+sqrt(rc2**2-(magl/2)**2)
  _y(4)=magl/2
 *else
  _err=3
  _x(4)=0
  _y(4)=0
 *endif
*elseif,magh,ne,0,then
 *if,_magflat,eq,0,then
  /com, top of the magnet is curved
  ! the constant radius option
  *if,magw,ne,0,then
   !  magnet width is by the angle 
   _x(4)=(rr2+magh)*cos(magw/nrp)
   _y(4)=(rr2+magh)*sin(magw/nrp)
  *elseif,magl,ne,0,then
   _x(4)=sqrt((rr2+magh)**2-(magl/2)**2)
   _y(4)=magl/2
  *else
   _err=3
   _x(4)=0
   _y(4)=0
  *endif
 *endif
 *if,_magflat,eq,1,then
  /com, top of magnet is flat
  *if,magw,ne,0,then
   !  magnet width is by the angle 
   _x(4)=_x(3)              !  (rr2+magh)*cos(magw/nrp)
   _y(4)=rr2*tan(magw/nrp)  ! (rr2+magh)*sin(magw/nrp)
  *elseif,magl,ne,0,then
   _x(4)=_x(3)      !  sqrt((rr2+magh)**2-(magl/2)**2)
   _y(4)=magl/2
  *else
   _err=3
   _x(4)=0
   _y(4)=0
  *endif
 *endif
*else
  _err=3
  _x(4)=0
  _y(4)=0
*endif



_x(5)=rr2
_y(5)=0

*if,loaf,eq,1,then

  _x(6)=sqrt(rr2**2-_y(4)**2)
  _y(6)=_y(4)

*else
 _x(6)=rr2*cos(magw/nrp)
 _y(6)=rr2*sin(magw/nrp)
*endif

*if,m_flat,eq,1,then
  /com,the bottom of the magnet is flat-it is not on the
  /com,curvature of the rotor surface                    5/29/99
  *if,magh,ne,0,then
   _x(6)=_x(4)-magh
  *else    !  12/23/00
   /com, magnet bottom is flat, using rr2 for position
   _x(6)=rr2
  *endif
  _x(5)=_x(6)
  _r7=sqrt(_x(6)**2+_y(6)**2)
  _x(7)=(_r7)*cos(_60/nrp)
  _y(7)=(_r7)*sin(_60/nrp)

  _x(8)=rr1
  _y(8)=0

*else
  _x(7)=(rr2)*cos(_60/nrp)
  _y(7)=(rr2)*sin(_60/nrp)

  _x(8)=rr1
  _y(8)=0
*endif

!_x(7)=(rr2)*cos(_60/nrp)
!_y(7)=(rr2)*sin(_60/nrp)

!_x(8)=rr1
!_y(8)=0

_x(9)=(rr1)*cos(_60/nrp)
_y(9)=(rr1)*sin(_60/nrp)

*if,rc1+rc2,ne,0,then
 _x(10)=rc1
 _y(10)=0
*else
 _x(10)=0
 _y(10)=0
*endif

_r_1=sqrt(_x(4)**2+_y(4)**2)
_x(11)=_r_1*cos(_60/nrp)
_y(11)=_r_1*sin(_60/nrp)

csys
ksel,none
*do,_ikp,1,_mxkp
/gopr
 k,,_x(_ikp),_y(_ikp)
 _kp_n(_ikp)=kp(_x(_ikp),_y(_ikp),0)
*enddo
cm,rot_k,kp

!  the  air gap next to the magnet
asel,none
lsel,none
csys,1
l,_kp_n(1),_kp_n(2)
l,_kp_n(2),_kp_n(11)
l,_kp_n(11),_kp_n(4)


_done=0
*if,rc1+rc2,ne,0,then
 *if,_magflat,eq,0,then
   larc,_kp_n(4),_kp_n(3),_kp_n(10),rc2
   _done=1
 *endif
*endif
*if,_magflat,eq,1,then            !  9/16/99
 /com, top of magnet is flat          !  9/16/99
 csys                                 !  9/16/99
 l,_kp_n(4),_kp_n(3)                  !  9/16/99
*elseif,_done,eq,0,then
 /com, top of magnet is curved-the default
 csys,1
 l,_kp_n(4),_kp_n(3)
 csys
*endif

l,_kp_n(3),_kp_n(1)
a,_kp_n(1),_kp_n(2),_kp_n(11),_kp_n(4),_kp_n(3),
*get,_ar_1,area,,num,max
!*if,embed,eq,0,then
 aatt,1,5,_elctyp
!*else
! aatt,2,2,_elctyp
!*endif
ksel,,,,_kp_n(3)
ksel,a,,,_kp_n(4)
ksel,a,,,_kp_n(11)
lslk,,1
*get,_lsmx,line,,num,max
*get,_lsmn,line,,num,min
lccat,_lsmn,_lsmx
cm,rot_a,area
!   the air beside the magnet
ksel,all
asel,none
lsel,none
csys,1
l,_kp_n(11),_kp_n(7)
l,_kp_n(7),_kp_n(6)
csys
l,_kp_n(6),_kp_n(4)
a,_kp_n(4),_kp_n(11),_kp_n(7),_kp_n(6),

*if,embed,eq,0,then
 aatt,1,5,_elmtyp
*else
 aatt,2,2,_elmtyp
*endif
cmsel,a,rot_a
cm,rot_a,area

!  the magnet 
! locate the next available CS, above 50
! *do,_i1,50,100                ! removed 2/15/96
!  *get,_esys,cdsy,_i1,attr,kcs ! removed 2/15/96
!  *if,_esys,eq,-1,exit         ! removed 2/15/96
! *enddo                        ! removed 2/15/96
! _mag_esy=_i1                  ! removed 2/15/96
! _esys=     $   _i1=           ! removed 2/15/96

*get,_mag_esy,cdsy,,num,max     !  added 2/15/96
*if,_mag_esy,lt,50,then         !  added 2/15/96
  _mag_esy=50                   !  added 2/15/96
*else                           !  added 2/15/96
  _mag_esy=_mag_esy+1           !  added 2/15/96
*endif                          !  added 2/15/96
 *if,rad_mag,eq,0,then
  local,_mag_esy,0
 *else
  rad_mag=1
  local,_mag_esy,rad_mag
*endif


 asel,none
 lsel,none
 ksel,,,,_kp_n(6)
 ksel,a,,,_kp_n(4)
 ksel,a,,,_kp_n(3)
 lslk,,1
 ksel,all
 l,_kp_n(3),_kp_n(5)
 csys,1
 *if,m_flat,eq,1,then       !  5/29/99
   csys                     !  5/29/99
 *endif                     !  5/29/99
 l,_kp_n(5),_kp_n(6)
 al,all
 aatt,3,3,_elctyp,_mag_esy
 cmsel,a,rot_a
 cm,rot_a,area

 !  the iron return path for the rotor
 asel,none
 lsel,none
 ksel,,,,_kp_n(5)
 ksel,a,,,_kp_n(6)
 ksel,a,,,_kp_n(7)
 lslk,,1
 ksel,all
 csys,1
 l,_kp_n(5),_kp_n(8)
 l,_kp_n(8),_kp_n(9)
 l,_kp_n(9),_kp_n(7)
 al,all

aatt,2,2,_elmtyp
cmsel,a,rot_a
cm,rot_a,area

!   set meshing divisions
!   number divisions in the radial gap
*set,_gapd
*dim,_gapd,,5
_gapd(1)=1,2,3,4,5
!   aspect ratio of tooth along the tooth face
*set,_asprg
*dim,_asprg,,5
_asprg(1)=2,2,1,1,1
!  factor applied to kesize for keypoints in the air gap
!  based on the gap/_gapd(arg1) value
*set,_kpz1
*dim,_kpz1,,5
_kpz1(1)=4,3,2,1,1
!  factor applied to kesize for keypoints at the back of the magnet 
*set,_kpz2
*dim,_kpz2,,5
! _kpz2(1)=7,5,4,3,1
_kpz2(1)=17,15,9,6,1
*voper,_kpz2(1),_kpz2(1),mult,f_2
!  factor applied to kesize for keypoints at the back of the iron
*set,_kpz3
*dim,_kpz3,,5
! _kpz3(1)=14,10,8,6,4
_kpz3(1)=24,20,18,6,4
*voper,_kpz3(1),_kpz3(1),mult,f_2
ksel,,,,_kp_n(1)
ksel,a,,,_kp_n(3)
lslk,,1
lesi,all,,,_gapd(arg1)

ksel,,,,_kp_n(11)
ksel,a,,,_kp_n(2)
lslk,,1
lesi,all,,,_gapd(arg1)

ksel,,,,_kp_n(1)
ksel,a,,,_kp_n(3)
ksel,a,,,_kp_n(2)
ksel,a,,,_kp_n(4)
ksel,a,,,_kp_n(11)
lslk,,1
lesi,all,_asprg(arg1)*(gap)/_gapd(arg1)/2   !   10/26/01

_gap_d=gap/_gapd(arg1)/2

ksel,all
kesi,_kp_n(3),_kpz1(arg1)*_gap_d
kesi,_kp_n(4),_kpz1(arg1)*_gap_d
kesi,_kp_n(11),_kpz1(arg1)*_gap_d

kesi,_kp_n(5),_kpz2(arg1)*_gap_d
kesi,_kp_n(6),_kpz2(arg1)*_gap_d
kesi,_kp_n(7),_kpz2(arg1)*_gap_d

kesi,_kp_n(8),_kpz3(arg1)*_gap_d
kesi,_kp_n(9),_kpz3(arg1)*_gap_d

cmsel,,rot_a
esha
*if,m_tri,eq,1,then
  esha,1
*endif
!   mesh the lower order elements first
cmsel,,rot_a
*if,arg2,eq,0,then
  asel,r,type,,_elmtyp
  ames,all
*endif
cmsel,,rot_a
*if,arg4,eq,0,then
  esha,2
  *if,m_tri,eq,1,then
   esha,1
  *endif
  ames,_ar_1
  esha
  *if,m_tri,eq,1,then
   esha,1
  *endif
  asel,r,type,,_elctyp
  ames,all
*else
  esha,2
  mshkey,1
  *if,m_tri,eq,1,then
   esha,1
  *endif
  ames,_ar_1
  esha
  mshkey
  *if,m_tri,eq,1,then
   esha,1
  *endif
*endif
cmsel,,rot_a
alis
ames,all

cmsel,,rot_a
esla
nsle
lsla
ksll
cm,rot_l,line
cm,rot_e,elem
cm,rot_n,node
cmgrp,rotor,rot_a,rot_l,rot_k,rot_e,rot_n
dsys
esel,r,mat,,6
cmsel,,rotor

*if,arg3,ne,0,then
!          1   2 3 4 5    6    7   8
  polegen,arg3,0,0,0,0,'rotor',0,ggeom
  /out,pm_rotor,out,,append
  csys,0
  mrk_nod=node(_x(3)*ggeom,_y(3)*ggeom,0)    !  marker node used in the 
                                             !  rotor movement
*endif


*if,rad_mag,eq,1,then
*get,_zz,mgxx,3
mp,mgxx,4,_zz*(-1)
*get,_zz,murx,3
mp,murx,4,_zz
_zz=
!   adjust the magnets to alternate in-out
_iflip=1
 *do,_i1,1,nrp
 /gopr
 *if,_iflip,eq,-1,then
   cmsel,,rotor
   esel,r,mat,,3
   nsle
   csys,1
   nsel,r,loc,y,360/nrp*((2*_i1-1)/2-1)-.00001,360/nrp*((2*_i1-1)/2)+.0001
   esln,,1
   emod,all,mat,4
!   eplo
   _iflip=_iflip*(-1)
 *else
   _iflip=_iflip*(-1)
 *endif
 *enddo
*endif
_iflip=  $ _zz=  $  _i1=

esel,,mat,,2,3         !  9/27/98   
cm,r_iron,elem       !  9/27/98   
r_iron_c='r_iron'    !  9/27/98   

cmsel,,rotor

/pnum,mat,1
csys
*get,_xmn,node,,mnloc,x
*get,_xmx,node,,mxloc,x
*get,_ymn,node,,mnloc,y
*get,_ymx,node,,mxloc,y
_dify=_ymx-_ymn
_difx=_xmx-_xmn
_eify=_ymx+_ymn
_eifx=_xmx+_xmn
_rmx=_dify
*if,_difx,gt,_dify,then
   _rmx=_difx
*endif
! /foc,1,_eifx/2,_eify/2
! /dis,1,1.05*_rmx/2
!  clean out the solid model
alls
cmsel,u,_cmp
!adel,all,,,1
!ldel,all,,,1
!kdel,all
! numc,kp
!numc,line
!numc,area
/auto
/num,1
eplo
immed,1


:err

/out