/out,slotrot,out
! *abbr,ames,stat
/com,  slotrot.mac     12/02/01  added center shaft if r_shaft=1
/com,  slotrot.mac     9/18/01
!   Generates the slotted rotor based on parameters
!   for the slotted rotor with a uniform shank.
!    arg1 = 0  the winding is stops at the back of the tooth face
!         > 0  the winding occupies the entire slot
!    arg2 = mesh refinement, 1 is minimum number of elements, 5 is most 
!           most refinement
!    arg3 = 0  use lower order element
!         > 0  use higher order element
!    arg4 = number of rotor teeth to be generated
!         = 0 (default), no action, no additional teeth are generated
!         ne 0 Build arg4 number of teeth
!    arg5 = 0 coil elements use the lower order elements
!         = 1 coil elements use the higher order elements
!    arg6 = 0 coil loads will be specified by currents 
!         = 1 coil load will be specified by voltages
!
!   Extra parameter input:
!   r_shaft=1, the area inside the inner radius of the rotor
!              is modeled as material 8
!
!
/uis,msgpop,3
!  /nerr,0,-1
fini
/prep7

_error=0
*get,_mod_id,parm,rotor_id,type
*if,_mod_id,eq,3,then


*if,rotor_id,ne,'slotrot',then
 /nerr,1,1e6
 /out
  *msg,error,rotor_id
 The model type ( %c ) does not correspond to a slotted rotor
 /out,slotrot,out,,append
 _error=1
*endif
*else
 _error=1
 /out
*msg,error
 The model type identifier specified is not a "character" type parameter&
 or it was not specified
 /out,slotrot,out,,append
*endif




*if,_error,eq,1,:err


/nerr,1,-1
!  geometry error check

_error=0

_nm_vn=9
*set,_nm_vv
*dim,_nm_vv,,_nm_vn
_nm_vv(1)=rr3,rr4,rr5,rspc,rt1,rc2,rth2,nrp,gap
*set,_nm_v
*dim,_nm_v,char,_nm_vn
_nm_v(1)='rr3','rr4','rr5','rspc','rt1','rc2','rth2','nrp','gap'
*do,_i,1,_nm_vn
*if,_nm_vv(_i),le,0,then
  *msg,error,_nm_v(_i)
  Parameter { %c }  is negative or zero.
  _error=_error+1
*endif
*enddo
 *if,rrs,lt,0,then
   _error=_error+1
  *msg,error
  The iron radius (RRS)  at the back of the slot cannot be negative
 *endif

!  default for the coil region of the rotor
*if,nconr,eq,0,then
  nconr=1
*endif

*if,_error,eq,0,then
 *if,rr4,lt,rr5,then
   _error=_error+1
  *msg,error
  The iron radius at the back of the slot must be larger than the &
  iron inner radius

 *endif
 *if,rr3,lt,rr4,then
   _error=_error+1
  *msg,error
  The slot inner radius must be smaller than the tooth&
  face radius

 *endif

 *if,rr4-rrs,lt,rr6,then
   _error=_error+1
  *msg,error
   The slot inner radius - rear slot radius must be&
   larger than the inner backiron radius.

 *endif
 *if,nrp*(rc2+rspc),gt,rr3*6.28,then
   _error=_error+1
  *msg,error
  The number of teeth and the spacing is larger than&
  the circumference at the tooth face.

 *endif
 *if,rth2,gt,90,then
   _error=_error+1
  *msg,error
  The rear tooth angle (RTH2) is greater than 90.

 *endif
 *if,rt1,gt,rc2/2,then
   _error=_error+1
  *msg,error
  The length of the side of the tooth face is&
  larger than the half thickness of the tooth&
  shank.

 *endif
*endif


*if,_error,eq,0,then
/nerr,0,1e4
*if,arg2,eq,0,then
 arg2=2
*endif
_cd1=1    !   radial divisons for the air space 

!   material numbers for the rotor
_rot_ir=2   !  rotor iron
_rot_cd=3   !  rotor conductor material




*get,_elmtyp,etyp,,num,max
_elmtyp=_elmtyp+1
_elctyp=_elmtyp+1

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

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

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

immed
esel,none
nsel,none
asel,none
ksel,none
lsel,none
cmsel,,rotor
cedel,all,,,any
cpdel,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

!   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
*if,r_shaft,eq,1,then
 _mxkp=23
*else
 _mxkp=22
*endif
*set,_kp_n_
*dim,_kp_n,,_mxkp
_60=180
*set,_x
*dim,_x,,_mxkp
*set,_y
*dim,_y,,_mxkp
*afun,deg
_x(1)=rr3
_y(1)=0
t_ht12=(180/3.141593)*(3.141593*rr3/nrp-rspc/2)/rr3
_x(2)=rr3*cos(t_ht12)
_y(2)=rr3*sin(t_ht12)
_x(3)=(rr3-rt1)*cos(t_ht12)
_y(3)=(rr3-rt1)*sin(t_ht12)
_y(4)=rc2/2
*if,rth2,ne,90,then
_x(4)=(rc2/2-_y(3))/tan(rth2)+_x(3)
*else
_x(4)=_x(3)
*endif
_x(5)=sqrt(rr4**2-(rc2/2)**2)
_y(5)=rc2/2
_x(6)=(rr4-rrs)*cos(_60/nrp)
_y(6)=(rr4-rrs)*sin(_60/nrp)
_x(7)=(rr5)*cos(_60/nrp)
_y(7)=(rr5)*sin(_60/nrp)
_x(8)=rr5
_y(8)=0
_x(9)=(rr3)*cos(_60/nrp)
_y(9)=(rr3)*sin(_60/nrp)
_x(10)=(rr3-rt1)*cos(_60/nrp)
_y(10)=(rr3-rt1)*sin(_60/nrp)
_x(11)=(_x(4)+_x(5))/2
_y(11)=rc2/2
_x(12)=_x(11)*cos(_60/nrp)
_y(12)=_x(11)*sin(_60/nrp)
rbc=rr3+gap/2
_x(13)=(rbc)
_y(13)=0
_x(14)=rbc*cos(t_ht12)
_y(14)=rbc*sin(t_ht12)
_x(15)=(rbc)*cos(_60/nrp)
_y(15)=(rbc)*sin(_60/nrp)
_x(16)=(rr4)*cos(_60/nrp)
_y(16)=(rr4)*sin(_60/nrp)
_x(17)=_x(4)
_y(17)=0
_x(18)=_x(5)
_y(18)=0
c_spc=(_x(17)-_x(18))/15
_x(19)=_x(11)+c_spc
_y(19)=0
_x(20)=_x(19)
_y(20)=rc2/2
_x(21)=(_x(11)+c_spc)*cos(_60/nrp)
_y(21)=(_x(11)+c_spc)*sin(_60/nrp)
_x(22)=_x(11)
_y(22)=0
*if,r_shaft,eq,1,then
 _x(23)=0
 _y(23)=0
*endif

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 tooth 
!  the tooth face
asel,none
lsel,none
csys,1
l,_kp_n(1),_kp_n(2)
l,_kp_n(2),_kp_n(3)
csys
l,_kp_n(3),_kp_n(4)
l,_kp_n(4),_kp_n(17)
l,_kp_n(17),_kp_n(1)
al,all
aatt,_rot_ir,1,_elmtyp
cm,rot_a,area

!  the tooth shank
*if,nconr,eq,1,then
 !  one phase per slot
 asel,none
 ksel,,,,_kp_n(17)
 ksel,a,,,_kp_n(4)
 lslk,,1
 ksel,all
 l,_kp_n(4),_kp_n(11)
 l,_kp_n(11),_kp_n(5)
 l,_kp_n(5),_kp_n(18)
 l,_kp_n(18),_kp_n(17)
 al,all
 !aatt,21,21,_elmtyp
 aatt,_rot_ir,1,_elmtyp
 cmsel,a,rot_a
 cm,rot_a,area
*else
!  two phases per slot
 asel,none
 ksel,,,,_kp_n(17)
 ksel,a,,,_kp_n(4)
 ksel,a,,,_kp_n(5)
 ksel,a,,,_kp_n(20)
 ksel,a,,,_kp_n(22)
 ksel,a,,,_kp_n(19)
 ksel,a,,,_kp_n(11)
 ksel,a,,,_kp_n(18)
 ksel,a,,,_kp_n(15)
 ksel,a,,,_kp_n(18)
 lslk,,1
 csys
 a,_kp_n(17),_kp_n(4),_kp_n(20),_kp_n(19)
 a,_kp_n(19),_kp_n(20),_kp_n(11),_kp_n(22)
 a,_kp_n(22),_kp_n(11),_kp_n(5),_kp_n(18)
 ! aatt,21,21,_elmtyp
 aatt,_rot_ir,1,_elmtyp
 cmsel,a,rot_a
 cm,rot_a,area
 ksel,,,,_kp_n(20)
 ksel,a,,,_kp_n(11)
 lslk,,1
 lesi,all,,,_cd1,,1
 ksel,,,,_kp_n(22)
 ksel,a,,,_kp_n(19)
 lslk,,1
 lesi,all,,,_cd1,,1
*endif

ksel,all
*if,rrs,ne,0,then
 larc,_kp_n(5),_kp_n(6),_kp_n(16),rrs
*else
 l,_kp_n(5),_kp_n(16)
*endif
*if,rr5,ne,0,then
  csys,1
  l,_kp_n(6),_kp_n(7)
  l,_kp_n(8),_kp_n(7)
*else
  l,_kp_n(6),_kp_n(8)
*endif
l,_kp_n(8),_kp_n(18)


!  the back iron of the yoke
lsel,none
asel,none
ksel,,,,_kp_n(18)
ksel,a,,,_kp_n(5)
*if,rrs,ne,0,then
 ksel,a,,,_kp_n(6)
*else
 ksel,a,,,_kp_n(16)
*endif
*if,rr5,ne,0,then
 ksel,a,,,_kp_n(7)
*endif
ksel,a,,,_kp_n(8)
lslk,,1
al,all
! aatt,21,22,_elmtyp
aatt,_rot_ir,1,_elmtyp
cmsel,a,rot_a
cm,rot_a,area



!  the coil region
lsel,none
asel,none
*if,nconr,eq,1,then
  ksel,all
  *if,rrs,eq,0,then
   l,_kp_n(10),_kp_n(16)
  *else
   l,_kp_n(10),_kp_n(6)
  *endif
  l,_kp_n(10),_kp_n(3)

  !  only one phase in the slot
  ksel,,,,_kp_n(10)
  ksel,a,,,_kp_n(3)
  ksel,a,,,_kp_n(4)
  ksel,a,,,_kp_n(11)
  ksel,a,,,_kp_n(5)
  *if,rrs,ne,0,then
   ksel,a,,,_kp_n(6)
  *else
   ksel,a,,,_kp_n(16)
  *endif
  lslk,,1
  al,all
  aatt,_rot_cd,1+_rl_strt,_elctyp
cmsel,a,rot_a
cm,rot_a,area
 *endif


lsel,none
asel,none
!  two phases to the slot
*if,nconr,eq,2,then
  !  two phases in the slot
  ksel,all
  *if,rrs,eq,0,then
   l,_kp_n(12),_kp_n(16)
  *else
   l,_kp_n(12),_kp_n(6)
  *endif
   csys,0     !   #1
!    csys,1
  l,_kp_n(11),_kp_n(12)
  l,_kp_n(20),_kp_n(21)
  l,_kp_n(10),_kp_n(21)
  l,_kp_n(10),_kp_n(3)


  csys,1
  !  the lower coil
  ksel,,,,_kp_n(10)
  ksel,a,,,_kp_n(3)
  ksel,a,,,_kp_n(4)
  ksel,a,,,_kp_n(20)
  ksel,a,,,_kp_n(21)
  lslk,,1
  al,all
  aatt,_rot_cd,1+_rl_strt,_elctyp
  cmsel,a,rot_a
  cm,rot_a,area
  asel,none
  !  the air between the two coils
  ksel,a,,,_kp_n(11)
  ksel,a,,,_kp_n(12)
  lslk,,1
  a,_kp_n(20),_kp_n(21),_kp_n(12),_kp_n(11)
  aatt,1,5,_elmtyp
  cmsel,a,rot_a
  cm,rot_a,area

  asel,none
  ksel,,,,_kp_n(11)
  ksel,a,,,_kp_n(12)
  ksel,a,,,_kp_n(5)
  *if,rrs,ne,0,then
   ksel,a,,,_kp_n(6)
  *else
   ksel,a,,,_kp_n(16)
  *endif
  lslk,,1
  al,all
  aatt,_rot_cd,2+_rl_strt,_elctyp
  cmsel,a,rot_a
  cm,rot_a,area
 ksel,,,,_kp_n(12)
 ksel,a,,,_kp_n(21)
 lslk,,1
 lesi,all,,,_cd1,,1
 *endif

!  the air gap 
ksel,all
l,_kp_n(2),_kp_n(9)
l,_kp_n(13),_kp_n(14)
l,_kp_n(14),_kp_n(15)
asel,none
a,_kp_n(1),_kp_n(13),_kp_n(14),_kp_n(2)
a,_kp_n(2),_kp_n(14),_kp_n(15),_kp_n(9)
aatt,1,5,_elmtyp
cmsel,a,rot_a
cm,rot_a,area

!  the slot at the side of the tooth face
asel,none
a,_kp_n(2),_kp_n(9),_kp_n(10),_kp_n(3)
*if,arg1,eq,0,then
  !  this is air
  aatt,1,5,_elmtyp
cmsel,a,rot_a
cm,rot_a,area
*else
  !  this is a part of the coil
  aatt,_rot_cd,1,_elmtyp
cmsel,a,rot_a
cm,rot_a,area
*endif

*if,r_shaft,eq,1,then
 asel,none
 a,_kp_n(23),_kp_n(8),_kp_n(7)
 aatt,8,8,1
 cmsel,a,rot_a
 cm,rot_a,area
*endif


!   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)=3,2,1,1,1
!  factor applied to kesize for keypoints at the back of the tooth face
!  based on the gap/_gapd(arg2) value
*set,_kpz1
*dim,_kpz1,,5
_kpz1(1)=6,2,2,1,1
!  factor applied to kesize for keypoints at the back of the tooth shank
*set,_kpz2
*dim,_kpz2,,5
_kpz2(1)=7,3,3,2,1
!  factor applied to kesize for keypoints at the back of the iron
*set,_kpz3
*dim,_kpz3,,5
_kpz3(1)=10,8,4,2,2

ksel,,,,_kp_n(1)
ksel,a,,,_kp_n(2)
kesi,_kp_n(1),gap/(_gapd(arg2)+1)    !  6/28/96
kesi,_kp_n(2),gap/(_gapd(arg2)+1)    !  6/28/96
lslk,,1
lesi,all,_asprg(arg2)*gap/_gapd(arg2)/2    !  6/28/96

ksel,,,,_kp_n(9)    !  6/28/96
kesi,_kp_n(9),gap/(_gapd(arg2)+1)    !  6/28/96

ksel,,,,_kp_n(2)
ksel,a,,,_kp_n(9)
lslk,,1
lesi,all,_asprg(arg2)*gap/_gapd(arg2)

ksel,,,,_kp_n(1)
ksel,a,,,_kp_n(13)
lslk,,1
lesi,all,,,_gapd(arg2)

ksel,,,,_kp_n(2)
ksel,a,,,_kp_n(14)
lslk,,1
lesi,all,,,_gapd(arg2)

*set,_kp_a1
*dim,_kp_a1,,6
_kp_a1(1)=1,13,2,14,9,15
ksel,none
*do,_ikp,1,8
 ksel,a,,,_kp_n(_kp_a1(_ikp))
*enddo
lslk,,1
asll,,1
esha,2
 ames,all

cmsel,,rot_a
lsla
ksll
_gap_d=gap/_gapd(arg2)
kesi,_kp_n(3),_kpz1(arg2)*gap
kesi,_kp_n(10),_kpz1(arg2)*gap
kesi,_kp_n(4),_kpz1(arg2)*gap
kesi,_kp_n(17),_kpz1(arg2)*gap
kesi,_kp_n(11),_kpz2(arg2)*gap
kesi,_kp_n(22),_kpz2(arg2)*gap
kesi,_kp_n(19),_kpz2(arg2)*gap
kesi,_kp_n(20),_kpz2(arg2)*gap
kesi,_kp_n(12),_kpz2(arg2)*gap
kesi,_kp_n(21),_kpz2(arg2)*gap
kesi,_kp_n(5),_kpz2(arg2)*gap
*if,rrs,ne,0,then
 kesi,_kp_n(6),_kpz2(arg2)*gap
*endif
kesi,_kp_n(16),_kpz2(arg2)*gap
kesi,_kp_n(18),_kpz2(arg2)*gap
*if,rr5,ne,0,then
 kesi,_kp_n(7),_kpz3(arg2)*gap
*endif
kesi,_kp_n(8),_kpz3(arg2)*gap

cmsel,,rot_a
esha
!   mesh the lower order elements first
cmsel,,rot_a
*if,arg3,eq,0,then
  asel,r,type,,_elmtyp
  ames,all
*endif
*if,arg5,eq,0,then
  asel,r,type,,_elctyp
  ames,all
*endif
cmsel,,rot_a
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,,20
cm,r_coil,elem
cmsel,,rotor

*if,r_shaft,eq,1,then
  _cpon=2
*else
 _cpon=0
*endif

!                                  rotation macro.
*if,arg4,ne,0,then
!           1  2    3     4 5    6      7     8
  polegen,arg4,0,'r_coil',0,1,'rotor',_cpon,ggeom
  /out,slotrot,out,,append      !   8/30/01
*endif
csys,0
mrk_nod=node(_x(1),_y(1),0)     !  marker node used in the 
immed

esel,r,mat,,1
emod,all,real,5

/com, update winding real sets    8/30/01
esel,,mat,,3
*get,_elmn0,elem,,num,min
*get,_creal0,elem,_elmn0,attr,real
_rlstr=_creal0-1
cm,r_coil,elem

csys,1
*do,_i1,1,nrpgen
 *msg,info,_i1
  cycle %i

  cmsel,,rotor
  esel,r,mat,,3
  nsle
  nsel,r,loc,y,360/nrp*(_i1-1)-.0001,360/nrp*(_i1)+.0001,
  esln,,1
  esel,r,mat,,3
  nsle
  *if,nconr,eq,1,then
    _rlstr=_rlstr+1
    emod,all,real,_rlstr
  *else
   /com, two conductors
   *get,_elmn,elem,,num,min
   *get,_creal,elem,_elmn,attr,real
   cm,_ce1,elem
   esel,r,real,,_creal
   _rlstr=_rlstr+1
   emod,all,real,_rlstr
   cm,_ce2,elem
   cm,_ce3,elem
   nsle
   esln
   esel,r,mat,,3
   esel,u,real,,_rlstr
   *get,_ecnt,elem,,count
   *get,_elmn1,elem,,num,min
   *get,_creal1,elem,_elmn1,attr,real
   *if,_ecnt,gt,0,then
     cmsel,,_ce1
     esel,r,real,,_creal1
     cmsel,a,_ce2
     !_rlstr=_rlstr+1
     emod,all,real,_rlstr
     cm,_ce3,elem
   *endif
   cmsel,,_ce1
   cmsel,u,_ce3
   _rlstr=_rlstr+1
   emod,all,real,_rlstr
  *endif
 *enddo
cmsel,,rotor

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

/pnum,mat,1
/num,1
eplo
immed,1
_xmn= $ _xmx= $ _ymn= $ _ymx= $ _difx=
_dify= $ _eifx= _eify= $ _rmx= $ *set,_kp_n
*set,_x $ *set,_y


*endif
:end
:err
/out