/out,slotsta,out
/com, slotsta.mac  10/26/01  mesh divisons changed/s_iron definition
/com, slotsta.mac  12/25/00
! *abbr,ames,stat
!   slotsta.mac
!   Generates the stator based on parameters
!    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
!         = 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
!
!    arg7 = 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
!
!    Other parameters:
!      t_hous > 0, builds an outer housing to the stator yoke  Material 7, real 7
!                  t_hous=thickness of the housing
!      t_air  > 0, puts an air gap between the outer housing and the outer
!                  radius of the stator yoke, material 1 , real 7
!                  t_air=thickness of the air
!
/uis,msgpop,3
/nerr,1,-1
fini
/prep7



_error=0
/nerr,1,1e6
*get,_mod_id,parm,stat_id,type
*if,_mod_id,eq,3,then
 *if,stat_id,ne,'slotsta',then
  /nerr,1,1e6
  /out
  *msg,error,stat_id
  The model type ( %c ) does not correspond to a slotted stator
  /out,slotsta,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,slotsta,out,,append
*endif


/nerr,0,1e6
*if,_error,eq,1,:err
/nerr
!   geometry check

_error=0

_nm_vn=7
*set,_nm_vv
*dim,_nm_vv,,_nm_vn
_nm_vv(1)=r3,r4,r5,tr1,tht2,nsp,gap
*set,_nm_v
*dim,_nm_v,char,_nm_vn
_nm_v(1)='r3','r4','r5','tr1','tht2','nsp','gap'
*do,_i,1,_nm_vn
*if,_nm_vv(_i),eq,0,then
  /out
  *msg,error,_nm_v(_i)
  Parameter { %c }  is negative or zero.
  _error=_error+1
*endif
*enddo
*if,c2+c3,eq,0,then
 *msg,error,_nm_v(_i)
  Parameter c2 and c3 are zero.  One of the dimensions for the tooth must&
  be nonzero.
  _error=_error+1
*endif

*if,_error,eq,0,then
 *if,r5,lt,r4,then
   _error=_error+1
  *msg,error
  The backiron outer radius must be larger than the back&
  iron inner radius

 *endif
 *if,r4,lt,r3,then
   _error=_error+1
  *msg,error
  The backiron inner radius must be larger than the tooth&
  face radius

 *endif

 *if,r4+rs,gt,r5,then
   _error=_error+1
  *msg,error
   The backiron inner radius + rear slot radius must be&
   smaller than the outer backiron radius.

 *endif
 *if,nsp*(c2+spc),gt,r3*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,tht2,gt,90,then
   _error=_error+1
  *msg,error
  The rear tooth angle (THT2) is greater than 90.

 *endif
 *if,tr1,gt,c2,then     !  1/6/97
   _error=_error+1
  *msg,error
  The length of the side of the tooth face is&
  larger than the thickness of the tooth&
  shank.

 *endif
*endif
!  for the extra flat
*if,flat,ne,0,then
  *if,(r5**2-(flat/2)**2),lt,0,then
    *msg,info
    The flat is too large
    _error=1
  *else
   y_2=sqrt(r5**2-(flat/2)**2)
   *if,y_2,lt,r4,then
    *msg,info
    The flat overlaps the inner radius of&
    the stator.
    _error=1
   *else
    r5_s=r5    !  save the input value for r5
    r5=(r4+y_2)/2
    r5_a=r5
   *endif
  *endif
*endif

/out,slotsta,out,,append
*if,_error,eq,0,then
/nerr,0,1e4

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

*if,arg2,eq,0,then
 arg2=2
*endif
_cd1=1    !   radial divisons for the air space 
!             between two phases in teh same slot

!             between two phases in the same slot
*get,_elmtyp,etyp,,num,max
*if,_elmtyp,eq,0,then
 _elmtyp=1          !  1/7/97    
*else               !  1/7/97    
_elmtyp=_elmtyp+1   !  1/7/97    
*endif
_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

*if,t_hous,gt,0,then
  _elhtyp=_elctyp+1
  *if,hous_cp,gt,0,then
   *if,arg3,eq,0,then
    et,_elhtyp,13,6
   *else
    et,_elhtyp,53,1
   *endif
  *else
   *if,arg3,eq,0,then
    et,_elhtyp,13
   *else
    et,_elhtyp,53
   *endif
  *endif
*endif


immed
esel,none
nsel,none
asel,none
ksel,none
lsel,none
cmsel,,stator
acle,all
adel,all,,,1
ldel,all,,,1
kdel,all
edel,all
ndel,all
 !numc,kp
!numc,line
!numc,area
numc,elem
numc,node


!   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
_alt=0
*if,c3,gt,0,then
 _alt=1
*endif


!  compute locations
_mxkp=22
*if,t_hous,gt,0,then
  *if,t_air,gt,0,then
     _mxkp=26   !   add the air gap between stator and housing
  *else
     _mxkp=24   !   no air gap
  *endif
*endif
*set,_kp_n_
*dim,_kp_n,,_mxkp
_60=180
*set,_x
*dim,_x,,_mxkp
*set,_y
*dim,_y,,_mxkp
*afun,deg
_x(1)=r3
_y(1)=0
t_ht12=(180/3.141593)*(3.141593*r3/nsp-spc/2)/r3
_x(2)=r3*cos(t_ht12)
_y(2)=r3*sin(t_ht12)
*if,_alt,eq,1,then
   _y(2)=c3/2-tr1*sin(tht2)
   _x(2)=sqrt(r3**2-_y(2)**2)
*endif
_x(3)=(r3+tr1)*cos(t_ht12)
_y(3)=(r3+tr1)*sin(t_ht12)
*if,_alt,eq,1,then
   _y(3)=c3/2
   _x(3)=_x(2)+tr1*cos(tht2)
*endif
_y(4)=c2/2
*if,_alt,eq,0,then
 *if,tht2,ne,90,then
  _x(4)=(c2/2-_y(3))/tan(180-tht2)+_x(3)
 *else
  _x(4)=_x(3)
 *endif
*else
 *if,tht2,ne,90,then
  _x(4)=(c2/2-c3/2)/tan(90+tht2)+_x(3)
 *else
  _x(4)=_x(3)
 *endif
*endif
*if,_alt,eq,0,then
 _x(5)=sqrt(r4**2-(c2/2)**2)
 _y(5)=c2/2
*else
  _tn2=tan(tht1)**2
  _a1=1+_tn2
  _b1=2*(_y(4)*tan(tht1)-_tn2*_x(4))
  _c1=_y(4)**2+_tn2*(_x(4)**2)-2*_x(4)*_y(4)*tan(tht1)-r4**2
  _x(5)=.5*(-_b1+sqrt(_b1**2-4*_a1*_c1))/_a1
  _y(5)=_y(4)+tan(tht1)*(_x(5)-_x(4))
*endif
_x(6)=(r4+rs)*cos(_60/nsp)
_y(6)=(r4+rs)*sin(_60/nsp)
_x(7)=(r5)*cos(_60/nsp)
_y(7)=(r5)*sin(_60/nsp)
_x(8)=r5
_y(8)=0
_x(9)=(r3)*cos(_60/nsp)
_y(9)=(r3)*sin(_60/nsp)
_x(10)=(r3+tr1)*cos(_60/nsp)
_y(10)=(r3+tr1)*sin(_60/nsp)
_x(11)=(_x(4)+_x(5))/2
*if,_alt,eq,0,then
 _y(11)=c2/2
*else
 _y(11)=(_y(4)+_y(5))/2
*endif
_x(12)=_x(11)*cos(_60/nsp)
_y(12)=_x(11)*sin(_60/nsp)
*if,rbc,le,0,then   !  12/25/00
  rbc=r3-gap/2
*endif
_x(13)=(rbc)
_y(13)=0
*if,_alt,eq,0,then
 _x(14)=rbc*cos(t_ht12)
 _y(14)=rbc*sin(t_ht12)
*else
  _ang2=atan(_y(2)/_x(2))
  _x(14)=rbc*cos(_ang2)
  _y(14)=rbc*sin(_ang2)
*endif

_x(15)=(rbc)*cos(_60/nsp)
_y(15)=(rbc)*sin(_60/nsp)
_x(16)=(r4)*cos(_60/nsp)
_y(16)=(r4)*sin(_60/nsp)
*if,_alt,eq,0,then
 _x(17)=_x(4)
*else
 _x(17)=sqrt(_x(4)**2+_y(4)**2)
*endif
_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)=c2/2
_x(21)=(_x(11)+c_spc)*cos(_60/nsp)
_y(21)=(_x(11)+c_spc)*sin(_60/nsp)
_x(22)=_x(11)
_y(22)=0
*if,t_air+t_hous,gt,0,then
  *if,t_air,gt,0,then
    /com, add the air gap
    _x(23)=_x(8)+t_air
    _y(23)=0
    _x(24)=(r5+t_air)*cos(_60/nsp)
    _y(24)=(r5+t_air)*sin(_60/nsp)
    _x(25)=_x(8)+t_air+t_hous
    _y(25)=0
    _x(26)=(r5+t_air+t_hous)*cos(_60/nsp)
    _y(26)=(r5+t_air+t_hous)*sin(_60/nsp)
  *else
    /com, no air gap
    _x(23)=_x(8)+t_hous
    _y(23)=0
    _x(24)=(r5+t_hous)*cos(_60/nsp)
    _y(24)=(r5+t_hous)*sin(_60/nsp)
  *endif
*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,sta_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)
*if,_alt,eq,1,then
 csys,1
*endif
l,_kp_n(4),_kp_n(17)
*if,_alt,eq,1,then
 csys
*endif
l,_kp_n(17),_kp_n(1)
al,all
aatt,5,5,_elmtyp
cm,sta_a,area

!  the tooth shank
*if,ncons,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,5,5,_elmtyp
 cmsel,a,sta_a
 cm,sta_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,5,5,_elmtyp
 cmsel,a,sta_a
 cm,sta_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,rs,ne,0,then
 /com adding additional curvature at the end of bottom of the slot   !  5/27/99
 csys                                                   !  5/27/99
 kdis,_kp_n(5),_kp_n(9)                                 !  5/27/99
 *if,rs,le,.98*_kdist,then                              !  5/27/99
   _rs=.98*_kdist                                       !  5/27/99
 *else                                                  !  5/27/99
   _rs=rs                                               !  5/27/99
 *endif                                                 !  5/27/99
 larc,_kp_n(5),_kp_n(6),_kp_n(16),_rs                   !  5/27/99
*else
 csys,1   !   12/2/98     
 l,_kp_n(5),_kp_n(16)
 csys     !   12/2/98     
*endif
*if,r5,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,rs,ne,0,then
 ksel,a,,,_kp_n(6)
*else
 ksel,a,,,_kp_n(16)
*endif
*if,r5,ne,0,then
 ksel,a,,,_kp_n(7)
*endif
ksel,a,,,_kp_n(8)
lslk,,1
al,all
aatt,5,5,_elmtyp
cmsel,a,sta_a
cm,sta_a,area



!  the coil region
lsel,none
asel,none
*if,ncons,eq,1,then
  ksel,all
  *if,rs,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,rs,ne,0,then
   ksel,a,,,_kp_n(6)
  *else
   ksel,a,,,_kp_n(16)
  *endif
  lslk,,1
  al,all
  aatt,6,1+_rl_strt,_elctyp
cmsel,a,sta_a
cm,sta_a,area
 *endif


lsel,none
asel,none
!  two phases to the slot
*if,ncons,eq,2,then
  !  two phases in the slot
  ksel,all
  *if,rs,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,6,1+_rl_strt,_elctyp
  cmsel,a,sta_a
  cm,sta_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,6,_elmtyp
  cmsel,a,sta_a
  cm,sta_a,area

  asel,none
  ksel,,,,_kp_n(11)
  ksel,a,,,_kp_n(12)
  ksel,a,,,_kp_n(5)
  *if,rs,ne,0,then
   ksel,a,,,_kp_n(6)
  *else
   ksel,a,,,_kp_n(16)
  *endif
  lslk,,1
  al,all
  aatt,6,2+_rl_strt,_elctyp
  cmsel,a,sta_a
  cm,sta_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,6,_elmtyp
cmsel,a,sta_a
cm,sta_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,6,_elmtyp
cmsel,a,sta_a
cm,sta_a,area
*else
  !  this is a part of the coil
  aatt,6,1,_elmtyp
cmsel,a,sta_a
cm,sta_a,area
*endif
/com, the housing
*if,t_air+t_hous,gt,0,then
  csys,1
  *if,t_air,gt,0,then
    /com, add the air gap
    asel,none
    a,_kp_n(8),_kp_n(23),_kp_n(24),_kp_n(7)   !  air gap
    aatt,1,7,_elmtyp
    cm,hous,area
    cmsel,a,sta_a
    cm,sta_a,area
    asel,none
    a,_kp_n(23),_kp_n(25),_kp_n(26),_kp_n(24)   !  housing
    aatt,7,7,_elhtyp
    cmsel,a,hous
    cm,hous,area
    cmsel,a,sta_a
    cm,sta_a,area
  *else
    /com, no air gap
    asel,none
    a,_kp_n(8),_kp_n(23),_kp_n(24),_kp_n(7)   !  housing
    aatt,7,7,_elhtyp
    cm,hous,area
    cmsel,a,sta_a
    cm,sta_a,area
  *endif
*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,4,2,1,1
*voper,_kpz1(1),_kpz1(1),mult,f_2
!  factor applied to kesize for keypoints at the back of the tooth shank
*set,_kpz2
*dim,_kpz2,,5
_kpz2(1)=7,5,3,2,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)=10,8,4,2,2
*voper,_kpz3(1),_kpz3(1),mult,f_2
ksel,,,,_kp_n(1)
ksel,a,,,_kp_n(2)
lslk,,1
lesi,all,_asprg(arg2)*(gap/2)/_gapd(arg2)   !  10/26/01
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(3)                            !  10/26/01
ksel,a,,,_kp_n(2)                           !  10/26/01
lslk,,1                                     !  10/26/01
lesi,all,_asprg(arg2)*(gap/2)/_gapd(arg2)   !  10/26/01

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

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


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/2)/_gapd(arg2)   !  10/26/01

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


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
*if,m_tri,eq,1,then
  esha,1
*endif
 ames,all

cmsel,,sta_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,rs,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,r5,ne,0,then
 kesi,_kp_n(7),_kpz3(arg2)*gap
*endif
kesi,_kp_n(8),_kpz3(arg2)*gap

*if,t_air+t_hous,gt,0,then
  n_hous=3  !  number of radial divisions in the housing
  *if,t_air,gt,0,then
   /com, air gap
   ksel,,,,_kp_n(8)
   ksel,a,,,_kp_n(23)
   lslk,,1
   lesi,all,,,1
   ksel,,,,_kp_n(8)
   ksel,a,,,_kp_n(24)
   lslk,,1
   lesi,all,,,1
!   kesi,_kp_n(23),_kpz3(arg2)*gap
!   kesi,_kp_n(24),_kpz3(arg2)*gap

   ksel,,,,_kp_n(25)
   ksel,a,,,_kp_n(23)
   lslk,,1
   lesi,all,,,n_hous
   ksel,,,,_kp_n(26)
   ksel,a,,,_kp_n(24)
   lslk,,1
   lesi,all,,,n_hous
!   kesi,_kp_n(25),_kpz3(arg2)*gap
!   kesi,_kp_n(26),_kpz3(arg2)*gap
  *else
   /com, no air gap
   ksel,,,,_kp_n(8)
   ksel,a,,,_kp_n(23)
   lslk,,1
   lesi,all,,,n_hous
   ksel,,,,_kp_n(8)
   ksel,a,,,_kp_n(24)
   lslk,,1
   lesi,all,,,n_hous
!   kesi,_kp_n(23),_kpz3(arg2)*gap
!   kesi,_kp_n(24),_kpz3(arg2)*gap
  *endif
*endif

cmsel,,sta_a
esha
*if,m_tri,eq,1,then
  esha,1
*endif
!   mesh the lower order elements first
cmsel,,sta_a
*if,arg3,eq,0,then
  asel,r,type,,_elmtyp
  *if,t_air+t_hous,gt,0,then
   cmsel,u,hous
  *endif
  ames,all
*endif
*if,arg5,eq,0,then
  asel,r,type,,_elctyp
  *if,t_air+t_hous,gt,0,then
   cmsel,u,hous
  *endif
  ames,all
*endif

cmsel,,sta_a
*if,t_air+t_hous,gt,0,then
 cmsel,u,hous
*endif

ames,all

/com, mesh##
*if,t_air+t_hous,gt,0,then
 n_hous=3  !  number of radial divisions in the housing
 ksel,,,,_kp_n(7)
 ksel,a,,,_kp_n(8)
 lslk,,1
 *get,_lsmx,line,,num,max
 *get,_cirdiv,line,_lsmx,attr,ndiv
 _cirdiv=abs(_cirdiv)
 ksel,,,,_kp_n(23)
 ksel,a,,,_kp_n(24)
 lslk,,1
 lesi,all,,,_cirdiv,,1
 *if,t_air,gt,0,then
  ksel,,,,_kp_n(25)
  ksel,a,,,_kp_n(26)
  lslk,,1
  lesi,all,,,_cirdiv,,1
 *endif
 cmsel,,sta_a
 asel,r,real,,7
 esha,2
 *if,m_tri,eq,1,then
  esha,1
 *endif
 ames,all
 esha
 *if,m_tri,eq,1,then
  esha,1
 *endif
*endif

cmsel,,sta_a
esla
nsle
lsla
ksll
cm,sta_l,line
cm,sta_e,elem
cm,sta_n,node
cmgrp,stator,sta_e,sta_n,sta_a,sta_l,sta_k
dsys
esel,r,mat,,6
cm,s_coil,elem
cmsel,,stator

*if,arg4,ne,0,then
!          1   1    3     4 5    6     7   8
  polegen,arg4,0,'s_coil',0,1,'stator',2,ggeom
 /out,slotsta,out,,append
*endif

! save,temp,db
*if,flat,ne,0,then
 !   add the flat
 !  rescale
 immed
 csys
 cmsel,,stator
 nscal,,all,,,1/ggeom,1/ggeom
 r5=r5_s
 g_flat
 /out,slotsta,out,,append

 cm,n_stator,node
 cm,e_stator,elem
 cmgrp,stator,n_stator,e_stator
 csys
 nscal,,all,,,ggeom,ggeom

*endif

*if,t_hous,gt,0,then
 *if,hous_cp,gt,0,then
  cmsel,,stator
  esel,r,mat,,7
  nsle
  *get,_cpmx,active,,cp
  _cpmx=_cpmx+1
  cp,_cpmx,volt,all
 *endif
*endif


!   apply az at outer nodes
cmsel,,stator
csys,1
*get,_rdmx,node,,mxloc,x
*get,_rdmn,node,,mnloc,x
_tolr=(_rdmx-_rdmn)/100
nsel,,ext
nsel,r,loc,x,_rdmx-_tolr,_rdmx+.1
d,all,az
/pbc,a,1
cmsel,,stator
esel,r,mat,,5,6
cm,s_iron,elem    !  10/28/01
cm,cstator,elem
!fmagbc,'cstator'
cmsel,,stator

!    _rtorq=ggeom*((r3-rbc)/2+rbc)
!   This parameter is calculated in POLEGEN.MAC
!
*get,_kpmx,node,,mxloc,x
/focu,1,0,0
/dis,1,1.1*_kpmx
/pnum,mat,1
/num,1
eplo
immed,1

*endif


*if,_idbug,eq,0,then
*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 $ _rdmn=
*endif

:err


/out