/out,coillink,out
/com, coillink.mac  11/06/01  allows calculation for rotor or stator coils   
/com, coillink.mac  9/30/01   original
!  computes the coil flux linkage based on the winding file and the 
!  current solution
!     arg1 = 0, do not load the data into the coillink array which was defined 
!               prior to using this macro (example:  rotsol1.mac)
!          > 0  location into which the flux linkage is to be loaded into the coillink array
!
!     arg2 = 0, no action
!          > 0  computes the EMF (V) from the 
!
!   parameter input:
!           hfreq   rotor mechanical frequency (HZ)
!
!
*if,hfreq,le,0,then
  hfreq=60
*endif

*set,link1
*dim,link1,,_n_slot

*set,link_str
*dim,link_str,,_mx_ph

cmsel,,stator
csys,1
*get,_thmn,node,,mnloc,y
*get,_thmx,node,,mxloc,y
_delaa=_thmx-_thmn
perfac=nint(360/_delaa)

!cmsel,,s_coil
esel,,mat,,3,6,3
etab,,a,z
etab,,volu
smult,azvolu,az,volu

*do,_il1,1,_n_slot
    /gopr
    *msg,info,_il1
    line number in winding file:  %i
    /com, process out slot
    _g_slot=cwnd_(_il1,1)
    cmsel,,%_g_slot%
    ssum
    *get,azt,ssum,,item,azvolu
    *get,tvolu,ssum,,item,volu
    azavout=azt/tvolu
    !_j_out=_cphc(wnd_(_il1,4))*wnd_(_il1,3)*_cpkamp
    !_j_out=_j_out/wnd_(_il1,5)
    /com, process in slot
    _g_slot=cwnd_(_il1,2)
    cmsel,,%_g_slot%
    ssum
    *get,azt,ssum,,item,azvolu
    *get,tvolu,ssum,,item,volu
    azavin=azt/tvolu
    !_j_in=-_cphc(wnd_(_il1,4))*wnd_(_il1,3)*_cpkamp
    !_j_in=_j_in/wnd_(_il1,6)
    link1(_il1)=wnd_(_il1,3)*(azavout-azavin)*stkthk*ggeom
    link_str(wnd_(_il1,4))= link_str(wnd_(_il1,4))+link1(_il1)
*enddo

*if,arg1,gt,0,then
  coil_lnk(arg1,0)=_angrot
  *do,_il1,1,_mx_ph
       coil_lnk(arg1,_il1)=link_str(_il1)
  *enddo
*else
 /nopr
 /out,coillink,sum
 *msg,info
 _______SUMMARY OF FLUX LINKAGE FOR THE COILS____________
 *msg,info 

 *msg,info,w_file
  Winding file:__________________ %c
 *msg,info,stkthk
  Stack length:__________________ %g
 *msg,info,perfac
  Periodic factor:_______________ %i
 *msg,info

 *msg,info
 ___COIL___Turns_____Flux linkage_______Phase____
 *msg,info
 ______________________Weber-t___________________
 *msg,info 

 *vwrit,sequ,wnd_(1,3),link1(1),wnd_(1,4)
 (f8.0,f8.0,f15.6,f13.0)
 *msg,info

 *msg,info

 *msg,info
 ____Phase_______Total Flux Linkage_______
 *msg,info

 *vwrite,sequ,link_str(1)
 (f10.0,f20.5)

 *if,perfac,gt,1,then
  *msg,info

  *msg,info
  The flux linkages reported are for the periodic section modeled.&
  The total flux linkages for each phase would be factored by the periodic factor
 *endif
 /out
 /gopr
 *uili,coillink,sum
*endif

*if,arg1,gt,0,then
 *if,arg1,eq,1,then
  /out,coillink,dat
 *else
  /out,coillink,dat,,append
 *endif
 *list,coillink,sum
*endif
/out