/out,ld_coil,out
/com,  ld_coil.mac
/com,   4/9/02    add check for 3D for Joule heating
/com,   3/25/02   add option of two sets of threee phase windings
/com,   3/23/02   add cosine form
!
!  Loads the coils generated by the b_wndsc.mac.  The phase shift
!  is assumed to be the 360 divided by the maximum phase number 
!  in the winding data.  If this is a 2D model and the winding
!  file has the wire guage defined the heat load is computed and
!  loaded into the macro joule_h.mac (To load the
!  heat load, enter joule_h at the command line.)
!
!  Can be used for 2D or 3D.
!  The winding specification is the same for 2D and 3D
!  but the method to implement the currents in the model
!  is different since 2D is based on the MVP and the 3D
!  is based on the scalar
!
!
!  arg1 = electrical angle
!  arg2 = peak current
!       = 0 resets the real constant sets to the orignal value
!           as generated in b_wndsc.mac for 3D models
!  arg3 = electric angle offset for the option of the second set of three phase windings
!         from the first set of the three phases.  To use this option, TWO_3PH=1 before 
!         this macro is called. 
!
!   other parameters:
!    from b_wndsc.mac
!        2D models
!         wnd_ array for the areas 
!        cwnd_ array for the element component names
!         _n_slot : number of entries in the winding file
!
!        3D models/scalar
!         wnd_ array for the winding and real constant data
!         _n_slot : number of entries in the winding file
!          
! 
_mx_ph=0
_err=0
*vscfun,_mx_ph,max,wnd_(1,4)
*if,_mx_ph,eq,0,then
  /nerr
  *msg,error
  The winding data does not exist, or it contains an error&
  In particular, there are no phases specfied.
  _err=1
*endif

/nerr,0,1e5
*if,_err,eq,1,:end

*if,_mx_ph,eq,2,then           !  12/03/00
 ph_shft=90                    !  12/03/00
*else                          !  12/03/00
 ph_shft=360/_mx_ph
*endif                         !  12/03/00

*afun,deg

/com, check for the currform parameter
*get,_tcurf,parm,currform,type
_setcurr=0
*if,_tcurf,eq,3,then
  /com, currform exists
  *if,currform,eq,'sine',then
   !  for the A phase
   _amp_th=.5
   *set,_amp_a
   *dim,_amp_a,table,2*720*_mx_ph/_amp_th
   !*vfill,_amp_a(1,0),ramp,0,.5
   *vfill,_amp_a(1,0),ramp,-720*_mx_ph,_amp_th
   *vfunc,_amp_a(1,1),sin,_amp_a(1,0)
   /title,Sine Current Input
  *elseif,currform,eq,'cos',then
   !  for the A phase
   _amp_th=.5
   *set,_amp_a
   *dim,_amp_a,table,2*720*_mx_ph/_amp_th
   !*vfill,_amp_a(1,0),ramp,0,.5
   *vfill,_amp_a(1,0),ramp,-720*_mx_ph,_amp_th
   *vfunc,_amp_a(1,1),cos,_amp_a(1,0)
   /title,COSine Current Input
  *elseif,currform,eq,'flat',then
   *set,_amp_a
   *dim,_amp_a,table,2
   _amp_a(1,0)=0,1e4
   _amp_a(1,1)=1,1
   /title, Flat Current Input
  *endif
  _setcurr=1
  *if,arg3,eq,1,then
   /grid,1
   /axlab,x,Electrical Angle (D)
   /axlab,y,Amp (A)
   /num,-1
   /xran
   /yran
   *vplo,_amp_a(1,0),_amp_a(1,1)
   /num,1
   /title, %mname%
  *endif
*endif

*if,_setcurr,eq,0,then
 *get,_tampa,parm,amp_a,type
 *if,_tampa,ne,2,then
  /nerr
  /out
  *msg,error
  The arrays for the current form have not been defined&
  the currform parameter has not been specified or was not&
  a valid currform
  /out,ld_coil,out,,append
  _err=1
 *else
  /com, the amp_a array exists-use it
  *get,_damp_a,parm,amp_a,dim,x
  *set,_amp_a
  *dim,_amp_a,table,6*_damp_a
  *vlen,_damp_a
  *vfunc,_amp_a(1,0),copy,amp_a(1,0)
  _phoff=-3*360
  *voper,_amp_a(1,0),_amp_a(1,0),add,_phoff
  *vlen,_damp_a
  *vfunc,_amp_a(1,1),copy,amp_a(1,1)
  *do,_ildc1,1,6
   _nstloc=_ildc1*_damp_a+1
   _ph_shft=360*(_ildc1-3) !  /_mx_ph
   *vlen,_damp_a
   *vfunc,_amp_a(_nstloc,0),copy,amp_a(1,0)
   *vlen,_damp_a
   *voper,_amp_a(_nstloc,0),amp_a(1,0),add,_ph_shft
   *vlen,_damp_a
   *vfunc,_amp_a(_nstloc,1),copy,amp_a(1,1)
  *enddo
 *endif
*endif

/nerr,0,1e5
*if,_err,eq,1,:end
!
_arg1=0
_arg2=0
_arg3=0

*if,arg9,eq,1,then
  *ask,_arg1,Electrical angle,0
  *ask,_arg2,Enter Peak Current,1
  *if,two_3ph,eq,1,then
   *ask,_arg3,Enter Angle offset,0
  *endif
  arg1=_arg1
  arg2=_arg2
  arg3=_arg3
*endif

_cpkamp=arg2
/solu
! determine the current phase current for each phase
*set,_char
*dim,_char,char,21
_char(1)='a','b','c','d','e','f','g','h','i','j'
_char(11)='k','l','m','n','o','p','q','r','s','t'
_char(21)='u'
*set,_cphc
*dim,_cphc,,_mx_ph
*set,_indx
*dim,_indx,,_mx_ph
*vfill,_indx(1),ramp,1,1
_arg1=arg1
*set,_indx1
*dim,_indx1,,_mx_ph

*if,arg2,ne,0,then
 !*vfill,_indx1(1),ramp,arg1,ph_shft
 ! *vfill,_indx1(1),ramp,arg1,ph_shft
 *if,_mx_ph,eq,1,then
  _indx1(1)=arg1
 *elseif,_mx_ph,eq,2,then
  _indx1(1)=arg1
  _indx1(2)=arg1+90
 *elseif,_mx_ph,eq,3,then
  _indx1(1)=arg1
  _indx1(2)=arg1-120
  _indx1(3)=arg1+120
 *elseif,two_3ph,eq,1,then
  *if,arg3,eq,0,then
     arg3=0
  *endif
  _indx1(1)=arg1
  _indx1(2)=arg1-60
  _indx1(3)=arg1-120
  _indx1(4)=arg1+arg3
  _indx1(5)=arg1-60+arg3
  _indx1(6)=arg1-120+arg3
 *else
  *vfill,_indx1(1),ramp,arg1,ph_shft
 *endif

 *stat,_indx1

 /nopr
 /out,ldt,mac
 *vlen,_mx_ph
 !  *vwrit,_indx(1),_char(1),_indx1(1)
 !  ('_cphc(',f4.0,')=amp_',a1,'(',f10.2,')')
  *vwrit,_indx(1),_indx1(1)
  ('_cphc(',f4.0,')=_amp_a(',f10.2,')')
 /out,ld_coil,out,,append
 /gopr
 ldt
*else
 _cpkamp=1
 *do,_ilo1,1,_mx_ph
   _cphc(_ilo1)=1
 *enddo
*endif

*stat,_cphc
rlis
*get,_dimn,active,,solu,dimn        ! 1=axisym, 2=planar, 3=3d 

*if,_dimn,ne,3,then
  /com, 2D application
  *do,_il1,1,_n_slot
    /gopr

    *msg,info,_il1
    line number in winding file:  %i

    /com, process out slot
    _g_slot=cwnd_(_il1,1)
    *msg,info,_g_slot,wnd_(_il1,4),wnd_(_il1,3)
    out slot data: %c wnd(4)  %i  wnd(3) %i
    cmsel,,%_g_slot%
    _j_out=_cphc(wnd_(_il1,4))*wnd_(_il1,3)*_cpkamp
    _j_out=_j_out/wnd_(_il1,5)
    bfe,all,js,3,_j_out


    /com, process in slot
    _g_slot=cwnd_(_il1,2)
    *msg,info,_g_slot,wnd_(_il1,4),wnd_(_il1,3)
    IN slot data: %c wnd(4)  %i  wnd(3) %i
    cmsel,,%_g_slot%
    _j_in=-_cphc(wnd_(_il1,4))*wnd_(_il1,3)*_cpkamp
    _j_in=_j_in/wnd_(_il1,6)
    bfe,all,js,3,_j_in

  *enddo
  _g_c_res=0
  *if,wnd_(1,7),gt,0,then     !  3/20/01
   *if,_dimn,eq,2,then        !  4/9/02
     g_c_res
     _g_c_res=1
     /out.ld_coil,out,,append
   *endif
  *endif
*else
 /com, 3D application
 *do,_il1,1,_n_slot
  _camp1=_cphc(wnd_(_il1,4))*wnd_(_il1,3)*_cpkamp
  rmod,wnd_(_il1,5),2,_camp1
  rmod,wnd_(_il1,6),2,_camp1
 *enddo
 rlis
*endif
*get,_tcurr,parm,currform,type
/nopr
*if,_rotsol2,eq,1,then
 /out,ld_coil,sum,,append
 *msg,info, _meang
 Current mechanical angle: %g
*else
 /out,ld_coil,sum
*endif
*msg,info
_____CURRENTS APPLIED TO THE COIL______
*msg,info

*msg,info,n_wnd_f
Name of winding file:______ %c
*if,_tcurr,eq,3,then
 *msg,info,currform
 Current form:_____________ %c
*else
 *msg,info
 Current form:_____________ Defined in table AMP_A
*endif
*msg,info,arg1
Electrical angle:__________ %g
*if,two_3ph,eq,1,then
*msg,info,_mx_ph
 Number of phases modeled:_ %i
*msg,info
 Note:  The additional three phases were modeled as a three phase winding&
 which was offset from the first set of three phases
*msg,info,arg3
 Angular offset of the second set of 3 phases&
 from the first set:________ %g
*endif
*msg,info,_cpkamp
Peak Current (A):__________ %g

*msg,info

*msg,info
____Phase__ Phase_Factor__
*msg,info

*vwrite,sequ,_cphc(1)
(f10.0,f14.4)
*msg,info

*if,_g_c_res,eq,1,then                 !  4/9/02
 *msg,info
 The following coil resistances and RMS heat loads have&
 been generated. The RMS heat loads are stored in joule_h.mac&
 and can be used after the model have been converted to&
 a thermal model.
 *msg,info,cwall
 Wall time: %g  at time of heat generation input
 *msg,info


 *msg,info
 ___Coil____Resistance(O)____RMS Heat(W)_______
 *vwrit,sequ,c_heat(1,1),c_heat(1,2)
 (f8.0,f12.4,f16.1)

 *msg,info,nint(totheat*10)/10
 Total heat generation (W):________ %g
*endif
*msg,info
__________________________
/out
/gopr
*if,_mg1,eq,0,then
  *uili,ld_coil,sum
*endif
:end

/out