/out,slotrot,out
! *abbr,ames,stat
!   slotrot.mac
!   Generates the 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 stator 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-with VOLT DOF
!         = 1 coil elements use the higher order elements-with VOLT DOF
!         = 2 coil element use the lower order element withOUT VOLT DOF
!         = 3 coil element use the higher order element withOUT VOLT DOF
!    arg6 = 0 No action 
!         = 1 coil load will be specified by voltages
!             (If arg6=1, this will override arg5 to use the 53's)
!
/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=10
*set,_nm_vv
*dim,_nm_vv,,_nm_vn
_nm_vv(1)=rr3,rr4,rr5,rspc,rt1,rrs,rc2,rth2,nrp,gap
*set,_nm_v
*dim,_nm_v,char,_nm_vn
_nm_v(1)='rr3','rr4','rr5','rspc','rt1','rrs','rc2','rth2','nrp','gap'
*do,_i,1,_nm_vn
*if,_nm_vv(_i),eq,0,then
  *msg,error,_nm_v(_i)
  Parameter { %c }  is negative or zero.
  _error=_error+1
*endif
*enddo


*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,6
*elseif,arg5,eq,1,then
  et,_elctyp,53,1
*elseif,arg5,eq,2,then
  et,_elctyp,13
*elseif,arg5,eq,3,then
  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
_mxkp=22
*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
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


!   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)
lslk,,1
lesi,all,_asprg(arg2)*gap/_gapd(arg2)
kesi,_kp_n(1),gap/(_gapd(arg2)+1)    !  6/28/96
kesi,_kp_n(2),gap/(_gapd(arg2)+1)    !  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,6   !  1/7/97
 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

cmsel,,rotor

!                                  rotation macro.
*if,arg4,ne,0,then
  polegen,arg4,0,'r_coil',0,1,'rotor',0,ggeom
  /out,slortor,out,,append
  *if,arg6,le,0,then
   *if,arg5,lt,2,then
    cm,c_e,elem
    cmsel,,r_coil
    nsle
    *get,cpmx,active,,cp
    cpmx=cpmx+1
    cp,cpmx,volt,all
    cmsel,,c_e
    nsle
   *endif
  *endif
*endif
/out,slotrot,out,,append

cmsel,,rotor
esel,r,mat,,3   !    material number for rotor coil 1/6/97
cm,r_coil,elem
cmsel,,rotor

csys,0
mrk_nod=node(_x(1),_y(1),0)     !  marker node used in the 
cmsel,,rotor
esel,r,mat,,2
cm,r_iron,elem
cmsel,,rotor
r_iron_c='r_iron'

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


*endif

*if,_idbug,eq,0,then
_xmn= $ _xmx= $ _ymn= $ _ymx= $ _difx=
_dify= $ _eifx= _eify= $ _rmx= $ *set,_kp_n
*SET,_60 $  *SET,_ARG2 $  *SET,_ASPRG $   *SET,_BLNK
*SET,_CURCOIL $  *SET,_CNT1 $   *SET,_CNT $   *SET,_CD1 $   *SET,_ARG1
*SET,_DEC1  $ *SET,_DELTHT $  *SET,_C_MAT $   *SET,_CURNMN
*SET,_ELREAL $  *SET,_ELMX  $   *SET,_ELMTYP $  *SET,_ELMAT  $  *SET,_ELCTYP
*SET,_EXCOIL $  *SET,_ERROR $   *SET,_EXNMG $   *SET,_GAPD $  *SET,_GAP_D
*SET,_GGEOM $   *SET,_HPWP $    *SET,_I    $    *SET,_ICNT
*SET,_IKP   $   *SET,_IMTES  $  *SET,_IPM    $  *SET,_IREAL
*SET,_KPMX $    *SET,_KPZ1  $  *SET,_KPZ2
*SET,_KPZ3  $  *SET,_KP_A1 $  *SET,_KP_N  $   *SET,_MAGTY
*SET,_MCLAE $   *SET,_MXKP  $  *SET,_NDMX
*SET,_NDMX1 $   *SET,_NGENI $   *SET,_NGEPOL
*SET,_NMGCOM $  *SET,_NM_V  $  *SET,_NM_VV $   *SET,_NREAL
*SET,_NMAGM $   *SET,_NMGCM $   *SET,_NRP
*SET,_NUMM  $   *SET,_RDMX  $   *SET,_REAL
*SET,_REALMN $  *SET,_REALMX $  *SET,_REFNDI
*SET,_RELINC $  *SET,_RL_STRT $ *SET,_RTHTMX
*SET,_THTMN $   *SET,_THTMX $   *SET,_X $   *SET,_Y
*SET,C_SPC   $ *SET,T_HT12 $  *SET,_BLNK  $  *set,_MGDAT
*SET,_MGMAT  $ *SET,_NM_VN
*SET,_CPMX   $ *SET,_EIFY $ *SET,_RDMN
*SET,_ROT_CD $ *SET,_ROT_IR
*endif

:err


/out