/out,b_wndsc,out
/com, b_wndsc.mac  8/7/01
/com, b_wndsc.mac  3/20/01
!
!   For 3D models:
!   Builds the source36 elements for the coil cross sections based on the winding data
!   in the winding file and the parameters in the design parameter file.
!
!   For 2D models:
!   No elements are generated, but array data and component data are
!   generated to be used for the actual application of the current.
!   The macro does not apply any currents for the 2D application
!   The air in the gaps must be material 1.  The elements in the coil
!   region must have a different material number.  Loads the wire guage data
!   for heat generation calculation
!
!   arg1 = name of the winding file.  This is character data and 
!          must be enclosed in single quotes  
!   arg2 = name of element  component to define the coil(for 2D) or iron region (for 3D)
!
!          2D models:  arg2 is the name of element component for the elements 
!                      in the coil region. If model is generated by a template
!                      it is 's_coil'. By default this macro checks for
!                      this component. If the winding is to be applied
!                      to the rotor, the name of the coil element components
!                      is 'r_coil'
!
!          3D models:  arg2 is the name of the stator iron elements
!                      or the rotor iron elements if the coil is in the rotor
!                      If the winding is to be applied to the rotor, 
!                      the name of the element components is 'r_iron'
!                      This defaults to the component defined in the
!                      mach3d.mac, "s_iron"
!
!          The element component name is character data and must be
!          enclosed in single quotes
!
!   arg3 = axial clearance of stack and inner surface of 
!          the winding (parameter units)
!   arg4 = width of a single coil  (parameter units)
!   arg5 = height of a single coil (parameter units)
!   arg6 = angle from +X CCW to the center of slot Number 1 (degrees)
!          Defaults to center of the first slot above the +X axis and this
!          first slot is at .5/number of slots degrees CCW from the +X axis
!   arg7 = maximum OUTER radius of a slot (parameter units)
!   arg8 = number of slots in stator, defaults to the parameter "NSP"
!          if the coils are in the stator. 
!          If the coils are in the rotor, the number of slots
!          in the rotor MUST be entered for this arguement
!   arg9 = 0, delete existing sourc36 elements (3D models)
!       NE 0, DO not delete existing sourc36 elements
!   ar10 = 0, no action
!        = 1, the coils are in the rotor and are to be rotated with the
!             rest of the rotor when the rotor is rotated. (3D models)
!   ar11 =  0 Stack is a half model (3D models)      ! 9/6/98
!        NE 0 Stack is the complete axial model, no symmetry about Z=0
!   ar12 = 1, user is prompted for 2D application for the winding file,
!            assuming all other inputs are default.
!
!   Additional parameters for options:
!
!   GGEOM = conversion factor from parameter units to meters
!
!
!   winding data : (read from the winding file) 
!   wnd_(i,j),i=1  _n_slot  (number of entries in the file)
!   wnd_(i,1)   =  the slot number for the OUT entry of the coil 
!                  through the laminate.  The first use of the slot
!                  assumes that the coil occupies the back end of the
!                  slot
!   wnd_(i,2)   =  the slot number for the IN entry of the coil 
!                  through the laminate
!   wnd_(i,3)   =  number of turns for this coil
!   wnd_(i,4)   =  Phase (1,2,3,4,,,) Phase 1 usually corresponds to 
!                  Phase A, Phase 2 to Phase B...
!   For 2D models
!   wnd_(i,5)   =  area for the OUT slot for the coil
!   wnd_(i,6)   =  area for the IN slot for the coil
!   wnd_(i,7)   =  winding wire gauge   (12/10/00)
!   cwnd_(i,1)  =  component name for the elements in the OUT slot for this coil
!   cwnd_(i,2)  =  component name for the elements in the IN slot for this coil
!
!   For 3D models
!   wnd_(i,5)   =  real constant set number for the bar sourc36 of 
!                  the coil
!   wnd_(i,6)   =  real constant set number for the arc sourc36 of
!                  the coil
!
!   Macros used:
!         dparm3d   deletes a number of parameters
!
!       For 2D models
!         g_area     computes area for active elements
!
!       For 3D models
!         g_3dcoil   builds the coil for an entry in the winding file
!
*msg,info
 b_wndsc.mac  7/15/98
fini
/uis,msgpop,3
/nerr,0,1e5
/prep7
/gopr
!
_arg_1=arg1
*get,_dimn,active,,solu,dimn        ! 1=axisym, 2=planar, 3=3d 

*if,_n_prom,eq,0,then
 *if,ar12,eq,1,then
 *ask,_arg_1,Winding file in quotes,0
 *if,_dimn,eq,3,then
  *ask,_arg_2,Iron region element component,0
  *ask,_arg_3,winding axial clearance,0
  *ask,_arg_4,width of a single coil,0
  *ask,_arg_5,height of single coil,0
  *ask,_arg_6,angular location of first slot(CCW),0
  *ask,_arg_7,maximum outer radius of slot,0
  arg2=_arg_2
  arg3=_arg_3
  *if,arg3,le,0,then
    arg3=ax_clear
  *endif
  arg4=_arg_4
  *if,arg4,le,0,then
    arg4=coil_w
  *endif
  arg5=_arg_5
  *if,arg5,le,0,then
    arg5=coil_h
  *endif
  arg6=_arg_6
  *if,arg6,le,0,then
    arg6=180/nsp
  *endif
  arg7=_arg_7
  *if,arg7,le,0,then
    arg7=maxslotr
  *endif
 *endif
 *endif
*else                  !   8/7/01  start
  _arg_1=_in_wndc(1)
  arg2=_in_wndc(2)
  arg3=_in_wnd(1)
  arg4=_in_wnd(2)
  arg5=_in_wnd(3)
  arg6=_in_wnd(4)
  arg7=_in_wnd(5)
  arg8=_in_wnd(6)
  arg9=_in_wnd(7)
  ar10=_in_wnd(8)
  ar11=_in_wnd(9)
  ar12=_in_wnd(10)      !   8/7/01  end
*endif

arg1=_arg_1

_err=0
_arg1=arg1
*get,_t_parm,parm,_arg1,type
*if,_t_parm,lt,3,then
 /nerr
 *msg,error
 An invalid Winding File name was input.  The name of the file&
 must be enclosed in single quotes ( 'bdc_1' )
 _err=1
*endif

_arg2=arg2
*get,_t_parm,parm,_arg2,type
*if,_t_parm,lt,3,then
 /com, case: arg2 is not a valid character input: check the defaults
 *if,_dimn,eq,3,then
  *get,_t_siron,parm,s_iron_c,type
  *if,_t_siron,ne,3,then
   /nerr
   *msg,error
   An invalid element component name was input and the component defined by the&
   macro to generate the 2D/3D model is not defined. The name of the Component&
   must be character data and it must be enclosed in single quotes  's_iron'
   _err=1
  *else
    _arg2=s_iron_c
    arg2=_arg2
  *endif
 *endif
 *if,_dimn,ne,3,then
  _arg2='s_coil'
 *endif
*else
 /com, case:  arg2 is a valid character
 *get,_t_parm,comp,_arg2,type
 /nerr
 *if,_t_parm,ne,2,then
  *msg,error
  The component for the coil or iron elements must be an Element&
  component
  _err=1
 *else
  esel,none
  cmsel,,%_arg2%
  *get,_ecnt,elem,,count
  *if,_ecnt,eq,0,then
    /nerr
    *msg,error
    The Element component specified for the coil or iron elements&
    does not have any elements in it.
    _err=1
  *endif
 *endif
*endif

*if,arg8,le,0,then
 *get,_t_nsp,parm,nsp,type
 *if,_t_nsp,ne,0,then
  /nerr
  /out
  *msg,error
  The number of stator teeth was not input&
  and the parameter "NSP" does not exist.
  /out,b_wndsc,out,,append
  _nteeth=1
  arg8=1
  _err=1
 *else
  _nteeth=nsp
  arg8=nsp
 *endif
*else
  _nteeth=arg8
*endif

*if,arg6,le,0,then
 arg6=180/_nteeth
*endif


*if,_dimn,eq,3,then
*if,arg3,le,0,then
 /nerr
 /out
 *msg,error
 The Clearance between the stack and the winding was not input.
 /out,b_wndsc,out,,append
 _err=1
*endif
*if,arg4,le,0,then
 /nerr
 /out
 *msg,error
 The width of an individual coil was not input.
 /out,b_wndsc,out,,append
 _err=1
*endif

*if,arg5,le,0,then
 /nerr
 /out
 *msg,error
 The height of an individual coil was not input.
 /out,b_wndsc,out,,append
 _err=1
*endif

*if,arg7,le,0,then
 /nerr
 /out
 *msg,error
 The maximum radius of the slot was not input.
 /out,b_wndsc,out,,append
 _err=1
*endif
*endif  !  for _dimn=3

/nerr,0,1e5
*if,_err,eq,1,:exi
dparm3d


*get,_dimn,active,,solu,dimn        ! 1=axisym, 2=planar, 3=3d 
*if,_dimn,ne,3,then
  coilreg=_arg2
  _plt_cur=1      !  plot JS on coil region (for 2D only)
*endif

*if,_dimn,eq,3,then
 s_iron_c=arg2
 _plt_cur=0      !  DO NOT plot JS on coil region (for 2D only)
*endif

esel,none
cm,winding,elem

/com, read the winding file
esel,all
*get,_elmg,elem,,num,max
*set,_arg3
*dim,_arg3,,1
n_wnd_f=_arg1
_n_slot=0
*vread,_arg3(1),%_arg1%,wnd
(40x,f10.0)
_n_slot=_arg3(1)

*set,_arg3
*set,wnd_
*dim,wnd_,,_n_slot,7
*if,_n_slot,le,0,then
  /nerr
  /out
  *msg,error,_arg1
  The winding file( %c .wnd) is missing the number of lines&
  in the file.
  /out,b_wndsc,out,,append
  _err=1
*endif
/nerr,0,1e5
*if,_err,eq,1,:end

*vread,wnd_(1,1),%_arg1%,wnd
(f10.0)
*vread,wnd_(1,2),%_arg1%,wnd
(10x,f10.0)
*vread,wnd_(1,3),%_arg1%,wnd
(20x,f10.0)
*vread,wnd_(1,4),%_arg1%,wnd
(30x,f10.0)
*vread,wnd_(1,7),%_arg1%,wnd
(50x,f10.0)

_val=1
*do,_iw1,1,_n_slot
  _val=wnd_(_iw1,1)*wnd_(_iw1,2)*wnd_(_iw1,3)*wnd_(_iw1,4)
  *if,_val,le,0,exit
*enddo

*if,_val,le,0,then
  /nerr
  /out
  *msg,error,_arg1
  The winding file( %c .wnd) is inconsistent with the number&
  of lines specified. When reading the file, a blank line&
  or a read error was encountered.
  /out,b_wndsc,out,,append
  _err=1
*endif
/nerr,0,1e5
*if,_err,eq,1,:end

*if,wnd_(1,7),gt,0,then             !  12/06/00
  _c_gag=wnd_(1,7)
 *do,_i1,1,_n_slot
   *if,wnd_(_i1,7),le,0,then
     wnd_(_i1,7)=_c_gag
   *else
     _c_gag=wnd_(_i1,7)
   *endif
 *enddo
*endif

*if,_dimn,eq,3,then
*if,arg9,eq,0,then
  esel,,ename,,36
  nsle
  edel,all
  ndel,all
  nsel,all
  esel,all
  *get,_ncnt,node,,count
  *if,_ncnt,gt,0,then
   numc,elem
   numc,node
  *endif
*endif
*endif

*vscfun,_mx_ph,max,wnd_(1,4)
*set,_ph_n
*dim,_ph_n,char,21
_ph_n(1)='ph_a','ph_b','ph_c','ph_d','ph_e','ph_f','ph_g'
_ph_n(8)='ph_h','ph_i','ph_j','ph_k','ph_l','ph_m','ph_n'
_ph_n(15)='ph_o','ph_p','ph_q','ph_r','ph_s','ph_t','ph_u'
esel,none
*do,_iw2,1,_mx_ph
  cm,%_ph_n(_iw2)%,elem
*enddo

!*go,:end


!  _sl_c(i)=current number of coils in the ith slot
*set,_sl_c
*dim,_sl_c,,_nteeth

*if,_dimn,eq,3,then    !  3D winding

*get,_etyp,etyp,,num,max
_etyp=_etyp+1

_s_tol=.05*arg5
_flat=ar11   ! 9/6/98
/com, cycle through the entries in the winding data
*do,_iw1,1,_n_slot
!*do,_iw1,1,1
/gopr
!  build the coil-arguement list for g_coil3d
!    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
!    ar12 = option to NOT delete existing sourc36 elements => NE 0
!    ar13 = symmetry model condition for the coil
!234567890!234567890!234567890!234567890!234567890!234567890!234567890!234567890
!        1         2         3         4         5         6         7         8
 *msg,info,_iw1
 cycle: %i
 _1=wnd_(_iw1,1)
 _2=wnd_(_iw1,2)
 _3=wnd_(_iw1,3)
 _sl_c(_1)=_sl_c(_1)+1
 _sl_c(_2)=_sl_c(_2)+1
 _inr=(arg7-arg5/2-(_sl_c(_1)-1)*arg5)-_s_tol
 _our=(arg7-arg5/2-(_sl_c(_2)-1)*arg5)-_s_tol
 !         1  2  3   4   5     6    7   8    9    10   11   12  13
 esel,none
 g_3dcoil,_1,_2,_3,arg3,arg8,_inr,_our,arg4,arg5,arg6,_etyp, 1,_flat
 /out,b_wndsc,out,,append

 *if,_no_arc,eq,1,then
  /com, there is no arc
  wnd_(_iw1,5)=_rl_mx    !  real constant from g_3dcoil for the bar
  wnd_(_iw1,6)=0         !  no
 *else
  /com, there is an arc
  wnd_(_iw1,5)=_rl_mx-1    !  real constant from g_3dcoil for the bar
  wnd_(_iw1,6)=_rl_mx      !  real constant from g_3dcoil for the arc
 *endif

 esel,r,ename,,36
 elis
 _cn=_ph_n(wnd_(_iw1,4))
 cmsel,a,%_cn%
 cm,%_cn%,elem
 cmsel,a,winding
 cm,winding,elem
 *list,g_3dcoil,out
 *msg,info
 ***************************************************
*enddo  !  end of _iw1 loop

*if,ar10,eq,1,then
 cmsel,,winding
 nsle
 cmsel,a,n_rotor
 cm,n_rotor,node
 cmsel,a,e_rotor
 cm,e_rotor,elem
*endif

*endif  !  end of 3D branch


*if,_dimn,ne,3,then   !  2D winding

 *do,_iw0,1,_n_slot
  _1=wnd_(_iw0,1)
  _2=wnd_(_iw0,2)
  _sl_c(_1)=_sl_c(_1)+1
  _sl_c(_2)=_sl_c(_2)+1
 *enddo
 /com, generate the component names
 *vscfun,_mxcps,max,_sl_c(1)
 *set,cwnd_
 *dim,cwnd_,char,_n_slot*_mxcps*2,2
 *set,_sec_n
 *dim,_sec_n,char,_n_slot*_mxcps*2
 *set,_exist
 *dim,_exist,,_n_slot*_mxcps*2
 *vfill,_exist(1),ramp,1000,1
 /nopr
 /out,data
 *vwrit,_exist(1)
 (2x,'c',f5.0)
 /out,b_wndsc,out,,append
 /gopr
 *vread,_sec_n(1),data
 (2x,a5)
 *stat,_sec_n,1,10
 *set,_exist
 *dim,_exist,,_n_slot*_mxcps*2

 _delth=360/_nteeth
 !  wnd_(i,5)=area for out slot
 !  cwnd_(i,1)=name for the out slot
 !  wnd_(i,6)=area for in slot
 !  cwnd_(i,2)=name for the in slot
 !  
 _iname=0
 *do,_iw1,1,_n_slot  !  _iw1 = ith line in the winding file
  !*do,_iw1,1,1

  /gopr
  *msg,info,_iw1
  cycle in _IW1: %i
  _1=wnd_(_iw1,1)
  _2=wnd_(_iw1,2)
  _3=wnd_(_iw1,3)
  _4=wnd_(_iw1,4)
  _cn=_ph_n(wnd_(_iw1,4))

  /com, process the out slot
  _sang=(_1-1)*_delth
  _eang=_sang+_delth
  csys,1
  cmsel,,coilreg
  nsle
  nsel,r,loc,y,_sang,_eang
  esln,,1
  cm,c_slot,elem
  *if,_sl_c(_1),eq,1,then
   /com, only one coil in this slot
   g_area
   /out,b_wndsc,out,,append
   _iname=_iname+1
   cwnd_(_iw1,1)=_sec_n(_iname)
   cm,%_sec_n(_iname)%,elem
   wnd_(_iw1,5)=t_area
  *else
   /com, more than one coil in this slot
   *if,_exist(_1),gt,0,then
     /com, slot has already been processed once
     _exslot=_sec_n(_exist(_1))
     cmsel,u,%_exslot%
     esel,u,mat,,1
     _iname=_iname+1
     cwnd_(_iw1,1)=_sec_n(_iname)
     cm,%_sec_n(_iname)%,elem
     g_area
     /out,b_wndsc,out,,append
     wnd_(_iw1,5)=t_area
   *else
     /com, first time in this slot
     g_area
     /out,b_wndsc,out,,append
     nsle
     csys,1
     *get,_rdmx1,node,,mxloc,x
     *get,_c_cthx,node,,mxloc,y
     *get,_c_cthn,node,,mxloc,y
     *if,_c_cthx*_c_cthn,lt,0,then
      *if,_c_thx-_c_cthn,gt,90,then
         _c_cthn=_c_cthn+180
      *endif
     *endif
     _c_ave=.5*(_c_thx+_c_cthn)
     _nd_out=node(_rdmx1,_c_ave,0)
     esln
     cmsel,r,coilreg
     *get,_elmx1,elem,,num,max
     *get,_c_rlx,elem,_elmx1,attr,real
     cmsel,,c_slot
     esel,r,real,,_c_rlx
     _iname=_iname+1
     cwnd_(_iw1,1)=_sec_n(_iname)
     cm,%_sec_n(_iname)%,elem
     g_area
     /out,b_wndsc,out,,append
     wnd_(_iw1,5)=t_area
     _exist(_1)=_iname
   *endif
  *endif

  /com, process the IN slot
  _sang=(_2-1)*_delth
  _eang=_sang+_delth
  csys,1
  cmsel,,coilreg
  nsle
  nsel,r,loc,y,_sang,_eang
  esln,,1
  cm,c_slot,elem
  *if,_sl_c(_2),eq,1,then
   /com, only one coil in this slot
   g_area
   /out,b_wndsc,out,,append
   _iname=_iname+1
   cwnd_(_iw1,2)=_sec_n(_iname)
   cm,%_sec_n(_iname)%,elem
   wnd_(_iw1,6)=t_area
  *else
   /com, more than one coil in this slot
   *if,_exist(_2),gt,0,then
     /com, slot has already been processed once
     _exslot=_sec_n(_exist(_2))
     cmsel,u,%_exslot%
     esel,u,mat,,1
     _iname=_iname+1
     cwnd_(_iw1,2)=_sec_n(_iname)
     g_area
     /out,b_wndsc,out,,append
     cm,%_sec_n(_iname)%,elem
     wnd_(_iw1,6)=t_area
   *else
     /com, first time in this slot
     g_area
     /out,b_wndsc,out,,append
     nsle
     csys,1
     *get,_rdmx1,node,,mxloc,x
     *get,_c_cthx,node,,mxloc,y
     *get,_c_cthn,node,,mxloc,y
     *if,_c_cthx*_c_cthn,lt,0,then
      *if,_c_thx-_c_cthn,gt,90,then
         _c_cthn=_c_cthn+180
      *endif
     *endif
     _c_ave=.5*(_c_thx+_c_cthn)
     _nd_out=node(_rdmx1,_c_ave,0)
     esln
     cmsel,r,coilreg
     *get,_elmx1,elem,,num,max
     *get,_c_rlx,elem,_elmx1,attr,real
     cmsel,,c_slot
     esel,r,real,,_c_rlx
     _iname=_iname+1
     cwnd_(_iw1,2)=_sec_n(_iname)
     g_area
     /out,b_wndsc,out,,append
     cm,%_sec_n(_iname)%,elem
     wnd_(_iw1,6)=t_area
     _exist(_2)=_iname
   *endif
  *endif
 *enddo  !  end of _iw1 loop through the slots in the winding file
  /com, put the elements into the phase element components
  *do,_iw2,1,_mx_ph
   esel,none
    *do,_iw3,1,_n_slot
      /gopr
      *msg,info,_iw3
       _IW3: %i
      *if,wnd_(_iw3,4),eq,_iw2,then
       _cn=cwnd_(_iw3,1)
       cmsel,a,%_cn%
       _cn=cwnd_(_iw3,2)
       cmsel,a,%_cn%
      *endif
    *enddo
    cm,%_ph_n(_iw2)%,elem
  *enddo
  /com, put all phases into the winding component
  esel,none
  *do,_iw2,1,_mx_ph
   _cn=_ph_n(_iw2)
   cmsel,a,%_cn%
  *enddo
  cm,winding,elem
*endif   !  end of 2D branch

!   summarize the winding data and the leads for the groups

/nopr
/out,%_arg1%,sum
*msg,info
___________SUMMARY OF THE WINDING GENERATION___________
*msg,info

*msg,info,_arg1
 Name of winding file:______________ %c .wnd
*msg,info,_arg2
 Name of coil element component:____ %c
*msg,info,_mx_ph
 Number of Phases:__________________ %i
*msg,info,_n_slot
 Number of coils:___________________ %i

*if,_dimn,eq,3,then
 *if,ar10,eq,1,then
   *msg,info
   The COIL elements are a part of the rotor component&
   and will be rotated with the rest of the rotor elements
 *endif
*endif

*msg,info

*msg,info

*msg,info
Explanations for the Winding:
*msg,info
 The coils associated with each phase are contained in a separate&
 element component.  Phase 1 coils are contained in the component ph_a&
 and Phase 2 in ph_b, etc.  All elements to apply Amp-turns are&
 contained in the element component "winding"
*msg,info

*msg,info
 In the generation of the winding, certain data was also stored for use&
 during the solution phase to allow the currents to be applied to the&
 winding


/out
*if,_nomsg,eq,0,then
 *uili,%_arg1%,sum
*endif
/gopr

!  parsav,all,temp,par

/out,b_wndsc,out,,append
*if,_idbug,eq,0,then
 _arg1= $ _arg2= $ _t_parm=

*endif

:exi
cmsel,,%arg2%
cmsel,a,winding
nsle
/eshap,1
eplo
_err=
_arg1=
_arg2=
_t_parm=
:end
! parsav,all,b_wndsc,par

/out