/out,_pole,out
!   polegen.mac
!   Generates a series of poles from a base mesh.
!
!    arg1 = number of poles to be generated
!           (defaults to a full machine)
!
!    arg2 = slot position numbering scheme
!         = 0, one slot position per slot
!         = 1, two slot positions per slot (one on the right side of the slot
!              and one for the left side of the slot, mostly used for generator
!              applications)
!
!    arg3 = name of the component containing the coil (or coils) (if one)
!           defaults to NO coil present.
!
!    arg4 = 0, the given model represents half of a pole (default)
!         = 1, the given model represents a complete single pole
!
!    arg5 = 0, the center line of the magnet pole or the center line
!              of the coil is at the edge of 
!              the model (right lateral edge) (default)
!              (along the +X axis of the input pole)
!         = 1, The edge of the magnet pole or centerline of the tooth
!              is the right lateral edge of the model
!              (along the +X axis of the input pole)
!
!    arg6 = name of the component which will be associated with the generated
!           poles.
!
!    arg7 = 0, the inner most row of nodes are to be coupled (AZ)
!         = 1, the outer most row of nodes are to be coupled (AZ)
!         = 2, No nodes are to be coupled (AZ)
!
!    arg8 = conversion factor to scale the model to MKS
!         = 0  (default), no scaling
!    
!    arg9 = factor applied to "gap" in computing the radius for the torque
!           calculation.  (torque-radius="_RTORQ"=rbc+gap/arg9).  Arg9
!           defaults to -4
!
!   Required parameters:  (units must those of the model parameters)
!      gap   = radial air gap
!      rbc   = radial location of common boundary of rotor/stator model
!      ggeom = factor to convert model parameter units to MKS (if model 
!              parameters are in inches, ggeom=.0254)
!              
!
immed,0
fini
/prep7
real=
_curcoil=arg3
_hpwp=arg4
_mclae=0
*if,arg5,eq,0,then
_mclae=1
*endif

cm,_curarea,area
!   complete the initial cm's    12/15/95
lsla
ksll
cm,_curarea,area
cm,_curline,line
cm,_curkp,kp
cmgrp,_curgrp,_curarea,_curline,_curkp
!    12/15/95

cm,_curelem,elem
nsle
*get,_curnmn,node,,num,min
*msg,info,_curnmn
_curnmn:  %i
csys,1     !  1
*get,_thtmx,node,,mxloc,y
*get,_thtmn,node,,mnloc,y
_deltht=_thtmx-_thtmn
*if,abs(_deltht),gt,180,then
  *get,_mag9esy,cdsy,,num,max     !
  _mag9esy=_mag9esy+1
  local,_mag9esy,1,
  cscir,_mag9esy,1
  *get,_thtmx,node,,mxloc,y
  *get,_thtmn,node,,mnloc,y
  _deltht=_thtmx-_thtmn
  cscir,_mag9esy,0
*endif
_nrp=nint(360/_deltht)
*if,_hpwp,eq,0,then
  _nrp=_nrp/2
*endif
*if,arg1,eq,0,then
  arg1=_nrp
*endif


cm,_curnode,node
!   load the arrays
*set,_real
*dim,_real,,200
*set,_mgdat
*dim,_mgdat,,200,4
*set,_mgmat
*dim,_mgmat,,200
!  determine if any coils are present
_nreal=0
_blnk='   '
/nerr,0,1e4
_excoil=0
*if,_curcoil,ne,_blnk,then
_excoil=1
*endif
!   /nerr,1,1e4
*if,_excoil,eq,1,then
!  coil specified
cmsel,,_curcoil
cm,_tcoil,elem
!  determine/load the real constant data
*get,_cnt,elem,,count
*do,_icnt,1,_cnt
 *get,_elmx,elem,,num,max
 *if,_elmx,eq,0,exit
 *get,_elreal,elem,_elmx,attr,real
 _nreal=_nreal+1
 _real(_nreal)=_elreal
 cmsel,,_tcoil
 esel,u,real,,_elreal
 cm,_tcoil,elem
*enddo
*vscfun,_realmx,max,_real(1)
*vlen,_nreal
*vscfun,_realmn,min,_real(1)
cmsel,,_curelem
*endif
!  determine if any PM are present
mat=
cm,_curelm1,elem
esel,none
cm,_mag,elem
_imtes=0
_nmagm=0
cmsel,,_curelem
*get,_cnt,elem,,count
*do,_ipm,1,_cnt
/gopr
 *get,_cnt1,elem,,count
 *if,_cnt1,eq,0,exit
 *get,_elmx,elem,,num,max
 *get,_elmat,elem,_elmx,attr,mat
 *get,_mgxx,mgxx,_elmat
 *get,_mgyy,mgyy,_elmat
 *get,_mgzz,mgzz,_elmat
*if,abs(_mgxx)+abs(_mgyy)+abs(_mgzz),ne,0,then
!   PM data 
 _nmagm=_nmagm+1
 _mgmat(_nmagm)=_elmat
 *get,_cnt2,elem,,count
esel,r,mat,,_elmat    !  2/16
 cm,_curelm2,elem
 *do,_iesys,1,_cnt1
/gopr
    *get,_cnt3,elem,,count
    *if,_cnt3,eq,0,exit
    *get,_elmx3,elem,,num,max
    *get,_elsys,elem,_elmx3,attr,esys    !  2/16
    *get,_esysa,cdsy,_elsys,ang,xy
    _imtes=_imtes+1
    _mgdat(_imtes,1)=_elmat            !  material number
    _mgdat(_imtes,2)=_elsys            !  element coordinate number
    _mgdat(_imtes,3)=_esysa            !  the rotation angle for the CS
    *if,_elsys,ne,0,then
    cmsel,,_curelm2
    esel,u,esys,,_elsys
    *else
    *msg,warn,_elmat
For material %i , the element coordinate system is 0, which is not permitted.
    cmsel,,_curelm2
    esel,u,mat,,_elmat
    *endif
    cm,_curelm2,elem
 *enddo
cmsel,,_mag
esel,a,mat,,_elmat
cm,_mag,elem
*endif

cmsel,,_curelm1
esel,u,mat,,_elmat
cm,_curelm1,elem
*enddo

alls       !   1
*get,_ndmx1,node,,num,max

cmsel,,_curelem
nsle,s
*get,_curnmn,node,,num,min
_refndi=_ndmx1-_curnmn+1
csys,1
*get,_rthtmx,node,,mxloc,y
*if,abs(_rthtmx),gt,1e-4,then
*get,_mag8esy,cdsy,,num,max     !

*if,_mag8esy,lt,11,then     !   6/23/96
   _mag8esy=49              !   6/23/96
*endif                      !   6/23/96

_mag8esy=_mag8esy+1
local,_mag8esy,rad_mag,0,0,0,_rthtmx
*endif
alls       !   2
  modm,detach
cmsel,,_curelem
nsle
nsym,y,_refndi,all
esym,,_refndi,all
cm,_refelm,elem


*msg,info,arg2,_mclae,_nreal,_realmx
Updating coils: arg2,_mclae,_nreal _realmx:  %i  %i %i %i
esel,none
cm,_newcoil,elem
*if,_nreal,ne,0,then
_relinc=_realmx-_realmn+1     !  1/2/96
!   _relinc=_realmx           !  removed 1/2/96
cmsel,,_curcoil
cm,_newcoil,elem
!   update the real constants for the opposite side in the reflected slot
!   for the generator application
*set,_dec1
*dim,_dec1,table,6
_dec1(1,0)=-1,-.1,-.09,.5,.6,2
_dec1(1,1)=0,0,1,1,0,0
*do,_ireal,1,_nreal
 cmsel,,_refelm
 cmsel,u,_curelem
 esel,r,real,,_real(_ireal)
    *if,arg2+_mclae,ge,1,then    !  #1
!*if,arg2,ge,1,then    !  removed  1/15/96
   emod,all,real,_relinc+_real(_ireal)
 *endif
 cmsel,a,_newcoil
 cm,_newcoil,elem
*enddo

  *if,_mclae+arg2,ne,0,then    !  removed  12/6/95
!*if,arg2,ne,0,then    ! #1/15/96
_relinc=2*_realmx
   *endif

*endif

*set,_dec1
*dim,_dec1,table,4
_dec1(1,0)=-1,.1,.11,2
_dec1(1,1)=0,0,1,1

*if,_imtes,ne,0,then
!   update the magnet component after the reflection
*do,_mag,1,_nmagm
cmsel,,_refelm
esel,r,mat,,_mgmat(_mag)
cmsel,a,_mag
cm,_mag,elem
*enddo

*vscfun,_sysinc,max,_mgdat(1,2)
!  _sysinc=_sysinc+1   !   2/16/96
  _sysinc=_nmagm   !   2/16/96

_chgsgn=1
!   _rang=360/(_nrp/(_hpwp+1))
  _rang=360/_nrp
_nmtes=_imtes
*do,_imag,1,_nmagm
*do,_jmag,1,_nmtes
!   _curesy=_mgdat(_jmag,3)+180*(_chgsgn+1)/2+_rang*_imag
_curesy=(2*_rthtmx-_mgdat(_jmag,3))+180*_dec1(_hpwp+_mclae)
local,_sysinc+_mgdat(_jmag,2),rad_mag,0,0,0,_curesy
    _imtes=_imtes+1
    _mgdat(_imtes,1)=_mgdat(_jmag,1)       !  material number
    _mgdat(_imtes,2)=_sysinc+_mgdat(_jmag,2)   !  element coordinate number
    _mgdat(_imtes,3)=_curesy       !  the rotation angle for the CS
cmsel,,_refelm
cmsel,u,_curelem
esel,r,mat,,_mgdat(_jmag,1)
esel,r,esys,,_mgdat(_jmag,2)
emod,all,esys,_sysinc+_mgdat(_jmag,2)
*enddo

*enddo


*endif


cmsel,,_refelm
nsle
*get,_mag7esy,cdsy,,num,max     !
_mag7esy=_mag7esy+1
local,_mag7esy,1,0,0,0,_rthtmx
*get,_thtmx,node,,mxloc,y
*get,_thtmn,node,,mnloc,y
_deltht=_thtmx-_thtmn

!   *get,_cnt1,node,,count
!   *get,_ndmx1,node,,num,max
alls       !   3
*get,_ndmx,node,,num,max
_ngeni=_ndmx-_curnmn+1
cmsel,,_refelm
nsle
_ngepol=arg1/(_hpwp+1)
csys,1

*if,_ngepol,gt,1,then
ngen,_ngepol,_ngeni,all,,,0,_deltht

!   generate noncoil, nonPM elements
cmsel,,_refelm
cmsel,u,_mag
cmsel,u,_newcoil
egen,_ngepol,_ngeni,all
cm,_newrot,elem

!   generate coil elements
*if,_nreal,ne,0,then


cmsel,,_refelm
cmsel,r,_newcoil
egen,_ngepol,_ngeni,all,,,0,0,_relinc
cm,_newcoil,elem
cm,coil,elem
cmsel,a,_newrot
cm,_newrot,elem
*endif

!  generate PM elements

_magty=1
*if,_imtes*_hpwp,ne,0,then
_magty=-1
_chgsgn=-1        !  2/16
!   1 pole was given
*do,_imag,1,_ngepol-1
*do,_jmag,1,_imtes
/gopr
_curesy=_mgdat(_jmag,3)+180*(_chgsgn+1)/2+_rang*(_imag)*2
local,_sysinc*(1+_imag)+_mgdat(_jmag,2),rad_mag,0,0,0,_curesy
*enddo
_chgsgn=_chgsgn*(-1)
*enddo
*endif

*msg,info,_rang,_imtes
_rang: %g  _imtes: %i
*if,_imtes*_magty,gt,0,then
 !   1/2  pole was given
 _chgsgn=-1        !  2/16
 _cesymg=_mgdat(_imtes,2)
 _s_cesy=_mgdat(_imtes,3)
 *do,_imag,1,_ngepol-1
  *do,_jmag,1,_imtes
   /gopr
   !  _curesy=_mgdat(_jmag,3)+180*(_chgsgn+1)/2+_rang*_imag
   *if,_jmag,eq,1,then
     _curesy=_s_cesy
   *else
     _chgsgn=_chgsgn*(-1)    !  moved here 2/17/96
     _curesy=(_imag+1)*_rang+abs(_chgsgn-1)*180/2
     _s_cesy=_curesy
   *endif
   _cesymg=_cesymg+1

   *msg,info,_imag,_jmag,_chgsgn,_mgdat(_jmag,3),_curesy
   _imag,_jmag  %i %i  _chgsgn:  %i   _mgdat(,3): %g _curesy: %g
   *msg,info,_cesymg,_mgdat(_jmag,2)
   _cesymg; %i _mgdat(,2): %g

   local,_cesymg,rad_mag,0,0,0,_curesy
  *enddo
  !   _chgsgn=_chgsgn*(-1)    !  removed 2/17/96
 *enddo
   *if,abs(_ngepol*_deltht),gt,180,then
     !  the last cs for the full model##
     local,_cesymg,rad_mag,0,0,0,_mgdat(1,3)
   *endif
*endif

cmsel,,_refelm
cmsel,r,_mag
!  egen,_ngepol,_ngeni,all,,,0,0,0,_sysinc*2
egen,_ngepol,_ngeni,all,,,0,0,0,_imtes            !   2/17/96
cm,_mag,elem
cmsel,a,_newrot
cm,_newrot,elem

*endif

!  cmsel,,_newrot
!  nsle

/nerr,0,1e4
_exnmg=0
*if,arg6,ne,_blnk,then
_exnmg=1
_nmgcm=arg6
*endif
!   /nerr,1,1e4
*if,_exnmg,eq,0,then
  _nmgcm='rotor'    !  12/6/95
*endif
*set,_nmgcom
*dim,_nmgcom,char,5
_nmgcom(1)='n_','e_','k_','l_','a_'
*cfop,_temp
*vwrite,_nmgcom(1),_nmgcm
(a2,a6)
*cfcl
*vread,_nmgcom(1),_temp
(a8)
cmsel,,_curarea,area
cmsel,,_curline,line
cmsel,,_curkp,kp
cmsel,a,_curgrp
cm,%_nmgcom(1)%,node
cm,%_nmgcom(2)%,elem
cm,%_nmgcom(3)%,kp
cm,%_nmgcom(4)%,line
cm,%_nmgcom(5)%,area
cmgrp,%_nmgcm%,%_nmgcom(1)%,%_nmgcom(2)%,%_nmgcom(3)%,%_nmgcom(4)%,%_nmgcom(5)%
!   update the coil component    12/15/95
cmsel,,_curcoil
*get,_elmx,elem,,num,max
*get,_c_mat,elem,_elmx,attr,mat
cmsel,,%_nmgcm%
esel,r,mat,,_c_mat
cm,_curcoil,elem
cmsel,,%_nmgcm%
!   12/15/95

*if,_numm,eq,0,then
  _numm=1e-6
*endif
cmsel,a,_curgrp
lsla
ksll
csys
numm,node,_numm
! ,kp,_numm


csys,1
nsel,r,ext
*if,arg7,eq,0,then
  *get,_rdmn,node,,mnloc,x
  nsel,r,loc,x,_rdmn-.01,_rdmn+.00001
  cpdel,all,,,any
  *get,_cpmx,active,,cp
  _cpmx=_cpmx+1
  cp,_cpmx,az,all
  numc,cp
*elseif,arg7,eq,1,then
  *get,_rdmx,node,,mxloc,x
  nsel,r,loc,x,_rdmn-.01,_rdmn+.00001
  cpdel,all,,,any
  *get,_cpmx,active,,cp
  _cpmx=_cpmx+1
  cp,_cpmx,az,all
  numc,cp
*endif

_ggeom=arg8
*if,_ggeom,eq,0,then
 _ggeom=1
*endif

cmsel,,%_nmgcm%
csys,1
!   *get,_rtorq,node,,mxloc,x
!   _rtorq=_ggeom*(_rtorq-arg9/4)
*if,arg9,eq,0,then
  _rtorq=_ggeom*(rbc-gap/4)
*else
  _rtorq=_ggeom*(rbc+gap/arg9)
*endif

cmdele,_curarea
lsla    !  12/6/95
ksll    !  12/6/95
!adele,all
!ldele,all
!kdele,all

csys
cmsel,,%_nmgcm%
  nscal,,all,,,_ggeom,_ggeom,_ggeom

alls       !   4
numc,node
numc,elem

/pbc,cp,1
/pbc,ce,1
/pbc,a,1
/pnum,mat,1
/num,1

!

immed,1
!/dist,,.03
!/focus,,.01,.01
! eplot
/out