/out,g_3dcoil,out
/com,g_3dcoil.out
!   g_3dcoil.mac
!   Builds a single coil of sourc36 elements
!
!    arg1 = out slot
!    arg2 = in slot
!    arg3 = number of turns
!    arg4 = clearance of stack and winding (parameter units)
!    arg5 = number of slots
!    arg6 = radius of the coil in the "out" slot
!    arg7 = radius of the coil in the "in" slot
!    arg8 = width of the coil winding
!    arg9 = height of the coil winding
!    ar10 = angle from +X CCW to the center of slot Number 1
!    ar11 = element type number (if blank, uses the next higher elemketn
!           type number available
!    ar12 = 0 delete existing sourc36 elements
!        NE 0 DO NOT delete existing sourc36 elements
!    ar13 = 0, model is symmetrical about the Z=0 axis       !  9/6/98
!         = 1, model is not symmtrical about the Z=0 axis    !  9/6/98
!
!   Other parameters:
!     ggeom = converts the length units of the parameters to meters
!    
!
!   Model assumptions:
!     Only half the motor is modeled in the axial direction for the
!      symmetrical condition
!     Slot one is at the +X axis
!     Slots are counted in the CCW direction
!     Stator component contains all the stator elements
!     Air is material 1
!
!    output parameters:
!     _rl_mx  : real constant set for the arc,  (_rl_mx-1 is the real set
!               for the bar
!
fini
/prep7
*if,ar12,eq,0,then
 esel,,ename,,36
 nsle
 edel,all,
 ndel,all
 numc,node
 numc,elem
 edel,_etyp
 numc,type
*endif


_no_arc=0
*if,gen_arc,eq,0,then
*if,skew_ang,gt,0,then
  *if,skew_stk,eq,'stator',then
     _no_arc=1
  *endif
*endif
*endif

_aof=ar10
_deltht=360/arg5
_thtout=(arg1-1)*_deltht+_aof
_thtin=(arg2-1)*_deltht+_aof
_n_turn=arg3
_r_in=arg6*ggeom
_r_out=arg7*ggeom
_ww=arg8*ggeom
_wh=arg9*ggeom

nsel,all
*get,ndmx,node,,num,max

cmsel,,stator
esel,u,mat,,1
nsle
*get,zmax,node,,mxloc,z
*get,_zmin,node,,mnloc,z
_z_f=zmax+ggeom*arg4+_ww/2
*if,ar13,eq,0,then  !  9/6/98
 _z_b=-_z_f
*else               !  9/6/98
 _z_b=_zmin-(ggeom*arg4+_ww/2)    !  9/6/98
*endif              !  9/6/98
!
!  get the largest real constant set
cm,c_e,elem
esel,all
*get,_elmx,elem,,num,max
*set,_mskg
*dim,_mskg,,_elmx
*set,_el_rl
*dim,_el_rl,,_elmx
*vget,_mskg(1),elem,1,esel
*voper,_mskg(1),_mskg(1),gt,0
*vmask,_mskg(1)
*vget,_el_rl(1),elem,1,attr,real
*vmask,_mskg(1)
*vscfun,_rl_mx,max,_el_rl(1)
_rl_mx=_rl_mx+1
*if,ar11,le,0,then
 *get,_etyp,etyp,,num,max
 _etyp=_etyp+1
*else
 _etyp=ar11
*endif
et,_etyp,36
type,_etyp

*set,_c_a36
*dim,_c_a36,,3,4
!  i=1,3 for i,j,k nodes of each coil segment
!  J=1,4 for each part of the coil
*afun,deg
csys
esel,none
/com, the straight bar for the out slot
ndmx=ndmx+1
n,ndmx,_r_out*cos(_thtout),_r_out*sin(_thtout),_z_b+_ww/2
_c_a36(1,1)=ndmx
ndmx=ndmx+1
n,ndmx,_r_out*cos(_thtout),_r_out*sin(_thtout),_z_f-_ww/2
_c_a36(2,1)=ndmx
ndmx=ndmx+1
n,ndmx,_r_out*cos(_thtout-2),_r_out*sin(_thtout-2),0
_c_a36(3,1)=ndmx
r,_rl_mx,2,_n_turn,_wh,_ww
real,_rl_mx
e,_c_a36(1,1),_c_a36(2,1),_c_a36(3,1)


*if,_no_arc,eq,0,then
/com, the arc on the front side of the coil
ndmx=ndmx+1
_rddg=180/3.141593
_a_fac=1
*if,_thtout,lt,_thtin,then
  _a_fac=-1
*endif
_th_omx=_thtout+_a_fac*_rddg*_ww/2/_r_out
n,ndmx,_r_out*cos(_th_omx),_r_out*sin(_th_omx),_z_f
_c_a36(1,2)=ndmx
ndmx=ndmx+1
_th_imn=_thtin-_a_fac*_rddg*_ww/2/_r_in
n,ndmx,_r_in*cos(_th_imn),_r_in*sin(_th_imn),_z_f
_c_a36(2,2)=ndmx
ndmx=ndmx+1
n,ndmx,,,_z_f
_c_a36(3,2)=ndmx
_rl_mx=_rl_mx+1
r,_rl_mx,3,_n_turn,_wh,_ww
real,_rl_mx
e,_c_a36(1,2),_c_a36(2,2),_c_a36(3,2)
*endif

/com, the bar for the in slot
ndmx=ndmx+1
n,ndmx,_r_in*cos(_thtin),_r_in*sin(_thtin),_z_f-_ww/2
_c_a36(1,3)=ndmx
ndmx=ndmx+1
n,ndmx,_r_in*cos(_thtin),_r_in*sin(_thtin),_z_b+_ww/2
_c_a36(2,3)=ndmx
ndmx=ndmx+1
n,ndmx,_r_in*cos(_thtin+2),_r_in*sin(_thtin+2),0
_c_a36(3,3)=ndmx
*if,_no_arc,eq,0,then
 real,_rl_mx-1
*else
 real,_rl_mx    !  2/11/01
*endif
e,_c_a36(1,3),_c_a36(2,3),_c_a36(3,3)


*if,_no_arc,eq,0,then
/com, the arc for the back side return
ndmx=ndmx+1
n,ndmx,_r_out*cos(_th_omx),_r_out*sin(_th_omx),_z_b
_c_a36(2,4)=ndmx
ndmx=ndmx+1

n,ndmx,_r_in*cos(_th_imn),_r_in*sin(_th_imn),_z_b
_c_a36(1,4)=ndmx
ndmx=ndmx+1
n,ndmx,,,_z_b
_c_a36(3,4)=ndmx
real,_rl_mx
e,_c_a36(1,4),_c_a36(2,4),_c_a36(3,4)
*endif


*if,_plot,eq,1,then
  esel,,ename,,36
  nsle
  /pbc,real,1
  /auto
  eplo
*endif


*set,_mskg
*set,_el_rl
:end

/out