/out,rotsol1,out
/nerr,0,1e4
/uis,msgpop,3
/com,   rotsol1.mac  12/21/01    compute torque by line integral on rotor and stator side
/com,   rotsol1.mac  11_numcp,_delth,_delnai/01    saves all torques/ editorial/plots as computed torques
/com,   rotsol1.mac  10/26/01    uses chk_run to continue cycles, option to select torque calculation
/com,   rotsol1.mac  8/5/01      uses RPM
/com,   rotsol1.mac  4/15/01
!   Rotates the machine , with no load or with an existing current load to the
!   coils, solves and computes the torque and generates a plot for each rotor position
!   If the winding file data has been input by <WIND_2D>, the EMF will be computed. If the
!   EMF is to be computed, then no currents should be input.
!
!   The model is connected together  with CE's
!   The starting,ending, increment angles are specified
!   in the input
!
!   Note that the rotor and stator nodal forces are output to rotor.dat and stator.dat. 
!   These files are not overwritten, but are appended.
!
!   The plotted torque is by virtual work and the torqsum internal ANSYS macro.
!
!
!     arg1 = starting mechanical angle, if non zero, the rotor is first moved by this
!            amount CCW (arg1>0), and the first solution is at the starting position
!     arg2 = ending mechanical angle
!     arg3 = incremental mechanical angle  (default is zero)
!            (if arg3 = 0, only one step is performed, arg2 is ignored,
!            and by default arg3 = 0)
!     arg4 = element component name for the calculation of the torque
!            (must be enclosed in single quotes), defaults to 'r_iron'
!     arg5 = rotor frequency(RPM)(defaults to HFREQ,if HFREQ=0, defaults to 3600 RPM) 8/05/01
!     arg6 = 1,2,3,4,5   for plot option
!     arg7 = 1,2,3,4,5   for plot option
!     arg8 = 1,2,3,4,5   for plot option
!          = -1, prompts the user for the input.
!     ar10 = 0 or 3 uses Virtual work to compute the torque
!          = 2, use maxwells stress to compute the torque
!       
!
!
!   Parameters Required at the time the macro is called:
!            mname = name of db containing the unscaled, unmerged model

!         Definition of Plot Options:
!            1 = BSUM
!            2 = HSUM
!            6 = flux line plot
!
!   Additional parameters:                              Default:
!     b_mx = the maxmimum B value for the contours        2.8
!     h_mx = the maxmimum H value for the contours        25000.
!     mu_mx = the maxmimum Mu value for the contours      4500.
!     se_mx = the maxmimum energy value for the contours  5e6
!     ber_mx = the maxmimum normalized error norm value 
!              for the contours                           1.0
!
!   At the end of this macro, the above parameters are set
!   to values obtained in these solutions
!
!   Summary file output:
!       stator.dat           nodal force output for stator (see g_force.mac for definitions)
!       rotor.dat            nodal force output for rotor  (see g_force.mac for definitions)
!       torqmach.sum         Summarizes the solutions, torque.
!       cog.sum              Running summary of the progress of the solutions
!       tor_2d_"mname".log   Comparision of the torques computed for rotor/stator and MXWF and virtual work
!
!
!   Plot file output:  "mname".f33
!       plot 1:  element plot (materials)
!       plot 2,3,4... : the requested plots.  The plots are generated
!                       starting with the first angle (B,H,MU,MG_ENG,B_ERR)
!       last plot:  The torque as a function of the mechanical angle
!
!   array output:
!    _rotm(I,0) = index of angle (mechanical)
!    _rotm(I,1) = torque for each position (this torque corresponds to the component and calculation
!                 method specified in the input arguements arg4 and ar10. This torque is printed
!                 out in torqmach.sum)
!
!    m_rotm(i,0= index of the angle (mechanical)  (This array is printed out in tor_2d_"mname".log file)
!    m_rotm(i,j)= torque for each position
!        i=1,numcp  number of solutions
!        j = 1 torque on rotor by maxwells stress
!          = 2 torque on rotor by virtual work
!          = 3 torque on stator by maxwells stress
!          = 4 torque on stator by virtual work
!          = 5 torque by Line integral on rotor side
!          = 6 torque by Line integral on stator side

!
!    macros called:
!      fmagbc   applied force boundary conditions
!      mvrotor  rotates rotor and connects models with CEs
!      mvpsol   solves
!      g_force  writes nodal forces
!      torqsum  gets torque
!      coillink computes flux linkage 
!      g_mark   gets marker node number
!
!
!


_mg1=1         !  do not print out intermediate summaries
_tor_loc=1     !  evaluate close to the stator
_fg_tor=4e-6   !  add/substract to radius

_arg_1=
_arg_2=
_arg_3=
_arg_4=
_arg_5=
_arg_6=

*if,arg8,eq,-1,then
 *ask,_arg_1,Starting Mechanical Angle,0
 *ask,_arg_2,Ending Mechanical Angle,180
 *ask,_arg_3,Incremental Mechanical Angle,0     !   3/5/97
 *ask,_arg_4,Torque component name ,0
 *ask,_arg_5,Rotor Speed (RPM),0
 *ask,go_now,1 to continue OR 0 to Stop,0
 arg1=_arg_1
 arg2=_arg_2
 arg3=_arg_3
 arg4=_arg_4
 arg5=_arg_5
 arg6=1
 arg8=0
*else
 go_now=1
*endif

*if,go_now,eq,0,:end

*msg,info,arg1,arg2,arg3,arg4,arg5
  %g   %g   %g   %c   %g



tormeth=3      ! =0,1  Maxwell-line integral, = 2 Maxwell-torqsum, =3, VW
               !  to compute the torque
*if,ar10,gt,0,then
  tormeth=ar10
*endif


esel,none
cmsel,,stator
*get,_elems,elem,,count

esel,none
cmsel,,rotor
*get,_elemr,elem,,count
_err=0
*if,_elems*_elemr,le,0,then
  _err=1
  /nerr
  /out
  *msg,error  
  Part of the model is missing-no action
*endif
/nerr,0,1e5
*if,_err,gt,0,:end

*if,arg5,le,0,then
  *get,_thfreq,parm,hfreq,type
  *if,_thfreq,lt,0,then
    arg5=60
  *else            !  8/05/01
    arg5=hfreq/60  !  8/05/01
  *endif
*else              !  8/05/01
   arg5=arg5/60    !  8/05/01
*endif
_hfreq=arg5

alls
*get,c_ecnt,elem,,count
*get,c_ncnt,node,,count


/com, component definition for the force/torque
cmsel,,rotor
esel,r,mat,,2,3
cm,r_iron,elem

/com,air outside the rotor
esel,,mat,,2,3
nsle
esln
esel,r,mat,,1
nsle
nsel,r,loc,z,_zmin-.0000001,_zmax+.000001
esln,,1
esel,r,mat,,1
nsle
cm,tor_rot,elem
cmsel,a,tor_sta


/com, air outside the stator
csys
esel,,mat,,5
nsle
csys
*get,_zmin,node,,mnloc,z
*get,_zmax,node,,mxloc,z
esel,,mat,,5
nsle
esln
esel,r,mat,,1
nsle
nsel,r,loc,z,_zmin-.0000001,_zmax+.000001
esln,,1
esel,r,mat,,1
nsle
csys,1
*get,_rmax,node,,mxloc,x
cm,ce,elem
nsel,r,loc,x,_rmax-.000001,_rmax+1
esln,r
nsle
cm,ce1,elem
cmsel,,ce
cmsel,u,ce1
cm,tor_sta,elem

/com, get marker node number
g_mark
/out,rotsol1,out,,append

cycle=0
*get,_wall,active,,time,wall
/out,cog,sum
*msg,info
__RUNNING SUMMARY OF SOLUTIONS IN COGGING.MAC____
*msg,info

*msg,info,_wall
Starting Wall clock time:____________ %g
*msg,info

_t_wnd=0
*get,_t_wnd,parm,wnd_,type
*vscfun,_nwmin,min,wnd_(1,3)
_t_wnd=_t_wnd*_nwmin

/out,rotsol1,out,,append
!    clean up the parameter list, so that we do not run out of space
!d_parm

fini
/solu
alls
!bfedel,all,all
!bfelis,all,all

dsys
cmsel,,stator
csys,1
*get,_staang,node,,mnloc,y
*get,_endang,node,,mxloc,y
_delang=abs(_staang-_endang)
*if,_delang,gt,180.000001,then
  sym_fac=1
*else
  sym_fac=360/_delang
*endif
/gopr
*if,arg9,eq,0,then
  /com, using stored energy to compute the torque
  arg9=0
*else
  arg9=1
   /com, using COenergy to compute the torque
*endif
_plt_cur=0     !=1, =>  superimpose the current in the winding
*get,_rcpu1,active,,time,cpu
_nomsg=1    !  no message/prompt display

! *if,arg1,le,0,then
!   arg1=.001
! *endif
*if,arg2,eq,0,then
  arg2=arg1
*endif
*if,arg2,gt,360,then
  arg2=360
*endif
*if,arg3,eq,0,then    !  3/5/97   
  arg3=0              !  3/5/97   
*endif
_arg4=arg4
*get,_targ4,parm,_arg4,type
*if,_targ4,ne,3,then
  _arg4='r_iron'
*endif
_comtorq=_arg4
_me_elo=0


/sho,xxx
fini
/prep7
 alls

!adel,all,,,1
!ldel,all,,,1
!kdel,all


/uis,msgpop,3
/out,rotsol1,out,,append


*set,_offon
*dim,_offon,,8
*set,_plt_int
*dim,_plt_int,,3
_plt_int(1)=arg6,arg7,arg8,arg9
*set,_plt_opt
*dim,_plt_opt,,7
*do,_i_1,1,3
 *do,_i_2,1,7
   *if,_plt_int(_i_1),eq,_i_2,then
     _plt_opt(_i_2)=1
   *endif
 *enddo
*enddo

!***********
*if,abs(arg3),lt,.0001,then        !  3/5/97  
  !   one step                     !  3/5/97  
  _numcp=1                         !  3/5/97  
  _delth=0                         !  3/5/97  
  arg2=arg1                        !  3/5/97
  _delnai=0                        !  3/5/97
*else                              !  3/5/97  
 _numcp=nint((arg2-arg1)/abs(arg3))+1
 _delth=(arg2-arg1)/(_numcp-1)
 _delnai=_delth
 *msg,info,_numcp,_delth,_delnai
 _numcp,_delth,_delnai:  %g  %g  %g

*endif                             !  3/5/97  


_strnai=arg1
*set,_rphst
*dim,_rphst,,_numcp
*do,_i1n,1,_numcp
!   _rphst(_i1n)=arg1+(_i1n-1)*_delth
   _rphst(_i1n)=_strnai+(_i1n-1)*_delnai
*enddo
*stat,_rphst

!***********

*set,_anl_mx
*dim,_anl_mx,,5
*set,_rotm
*dim,_rotm,table,_numcp

*set,coil_lnk   !    emf versus angle
*dim,coil_lnk,table,_numcp,_mx_ph
coil_lnk(0,0)=1e-6
*do,_i1,1,_mx_ph
 coil_lnk(0,_i1)=_i1
*enddo

*set,_rot_m
*dim,_rot_m,table,_numcp,3
_rot_m(0,0)=1e-7
_rot_m(0,1)=1
_rot_m(0,2)=2
_rot_m(0,3)=3

_full=0
nsel,all
csys,1
*get,_thmn,node,,mnloc,y
*get,_thmx,node,,mxloc,y
*if,_thmx-_thmn,gt,180.0,then
  _full=1
*endif

fini
/solu
alls
_mg1=1
/nopr
/out,temp_,mac
*vwrit,_comtorq
('fmagbc,',1h',a8,1h')
/out,rotsol1,out,,append
/gopr
temp_
/out,rotsol1,out,,append
_mg1=1
*if,_arg4,ne,'r_iron',then
 fmagbc,'r_iron'
 /out,rotsol1,out,,append
*endif

*if,_arg4,ne,'s_iron',then
 fmagbc,'s_iron'
 /out,rotsol1,out,,append
*endif


*if,_nosolve,eq,0,then

! save,%mname%,db
*if,_full,eq,0,then
 /com, periodic model
 /auto
 mvrotor,arg2             !  10/29/00
 /out,rotsol1,out,,append
 /pbf,js,1
 eplo
 /user
 !mvrotor,_rphst(1)-_delth,1
 /out,rotsol1,out,,append
*else
 /com, full model
 csys,1
 *get,_cth0,node,mrk_nod,loc,y
 csys
 _cmv_0=arg1-_cth0-_delth
 *if,_cmv_0,ne,0,then
   /com, moving rotor to starting position less delta
   _mg1=1
   mvrotor,_cmv_0,1
   /out,rotsol1,out,,append
 *endif
*endif
/pbf,js
/sho,%mname%,f33

!        i=1,numcp  number of solutions
!        j = 1 torque on rotor by maxwells stress
!          = 2 torque on rotor by virtual work
!          = 3 torque on stator by maxwells stress
!          = 4 torque on stator by virtual work
!          = 5 torque by Line integral on rotor side
!          = 6 torque by Line integral on stator side

*set,m_rotm
*dim,m_rotm,table,_numcp,6
m_rotm(0,0)=1e-6
m_rotm(0,1)=1
m_rotm(0,2)=2
m_rotm(0,3)=3
m_rotm(0,4)=4
m_rotm(0,5)=5
m_rotm(0,6)=6


*do,_i_1,1,_numcp
 /gopr
 cycle=cycle+1
 *msg,info,cycle
  Solution No:_____ %i
 fini
 /prep7
*if,cw_mot,ne,0,then
  !  clockwise motion of the rotor
  _meang=_me_elo-_rphst(_i_1)
*else
  !  cw_mot = 0
  !  COUNTERclockwise motion of the rotor
  _meang=_me_elo+_rphst(_i_1)
*endif

 !  move the rotor
 *if,_full,eq,1,then
  /com, full model
  mvrotor,_delth,1        ! 10/22/96
  /out,rotsol1,out,,append
 *else
  /com, partial model
  mvrotor,_meang,1
  alls
  !eplo
  /out,rotsol1,out,,append
 *endif

 /uis,msgpop,3
 /out,rotsol1,out,,append
 /nerr,0,1e6,-1
 !esel,,mat,,5
 !cm,statiron,elem

 mvpsol
 /out,rotsol1,out,,append
 fini
 /post1
*if,_tor_loc,eq,0,then
 cmsel,,rotor
 esel,u,mat,,1
 nsle
 csys,1
 *get,_rdmx,node,,mxloc,x
 _rtorq=_rdmx+_fg_tor         !   12/1/96
 *msg,info,_fg_tor,_rdmx,_rtorq
 _fg_tor , _rdmx , _rtorq:  %g :  %g :  %g
*else
 cmsel,,stator
 esel,r,mat,,5
 nsle
 csys,1
 *get,_rdmn,node,,mnloc,x
 _rtorq=_rdmn-_fg_tor
*endif
 esel,,mat,,5
 cm,statiron,elem
 esel,all

_mg1=1
/nopr
/out,temp_,mac
*vwrit,_comtorq
('torqsum,',1h',a8,1h')
/out,rotsol1,out,,append
/gopr
temp_
/out,rotsol1,out,,append

!*if,tormeth,le,1,then 
! _torqcir,_rtorq,50     !  12/1/96
! /out,rotsol1,out,,append
!*endif
!   fini
!  _mg1=1
!  /post1
! torqsum,'r_iron'

 /gopr
 alls
 ! etab,,sene
 ! ssum
 c_eng=0
 s_eng=0
!  senergy,arg9                ! 10/26/96
! *if,arg9,eq,0,then           ! 10/27/96
!  t_sene=s_eng
! *else                        ! 10/27/96
!  t_sene=c_eng                ! 10/27/96
! *endif                       ! 10/27/96

csys,1
cmsel,,rotor
*get,_rmax,node,,mxloc,x
rtor1=_rmax-.5*.5*gap*ggeom
rtor2=_rmax+.5*.5*gap*ggeom

mtorqc2d,rtor1
/out,rotsol1,out,,append
torqmx1=torque

mtorqc2d,rtor2
/out,rotsol1,out,,append
torqmx2=torque






 g_force,,,_meang,cycle,_numcp
 /out,rotsol1,out,,append

 ! *get,t_sene,ssum,,item,sene
 *if,stkthk,le,0,then
   _stk=ggeom
 *else
   _stk=stkthk*ggeom
 *endif
 *msg,info,_i_1,torqmx,torqvw,torque
 I: %i (MX): %g  (VW): %g  (mx-li): %g

 *if,tormeth,le,1,then
  _torcal=torque*sym_fac
 *elseif,tormeth,eq,2,then
  _torcal=torqmx*sym_fac*_stk
 *else
  _torcal=torqvw*sym_fac*_stk
 *endif
m_rotm(_i_1,0)=_meang
torqsum,'r_iron'
m_rotm(_i_1,1)=torqmx*141.8*sym_fac*_stk
m_rotm(_i_1,2)=torqvw*141.8*sym_fac*_stk

torqsum,'s_iron'
m_rotm(_i_1,3)=torqmx*141.8*sym_fac*_stk
m_rotm(_i_1,4)=torqvw*141.8*sym_fac*_stk
m_rotm(_i_1,5)=torqmx1*141.8*sym_fac*_stk
m_rotm(_i_1,6)=torqmx2*141.8*sym_fac*_stk




 t_sene=t_sene*_stk*ggeom*sym_fac
 t_sene=1
 *get,_wall,active,,time,wall
 /out,cog,sum,,append
 *msg,info,cycle
 Cycle Number__________________________ %i
 *msg,info,_wall
 Current Wall Clock Time:______________ %g
 *msg,info,_meang
 Current mechanical angle of rotor:____ %g
 !*msg,info,t_sene
 !Total Energy (J):_____________________ %g
 *msg,info,_torcal*141.8
 Current torque by MXWF (oz-in):__________ %g

 ! *list,senergy,out
 *msg,info
  * * * * * * * * * * * * * * * * * * * * * *
 /out,rotsol1,out,,append
 /gopr
!  _rotm(_i_1,0)=_rphst(_i_1)   
! _angrot=nint(_meang/ang_inc)*ang_inc
_angrot=_meang
 _rotm(_i_1,0)=_angrot   !  11/14/96
 _rotm(_i_1,1)=_torcal*141.8
 _rot_m(_i_1,0)=_angrot     !  11/14/96
 _rot_m(_i_1,1)=t_sene

*if,_t_wnd,gt,0,then
 coillink,_i_1     !  compute and load the flux linkage based on the winding file
*endif
/out,rotsol1,out,,append

 alls
/out,rotsol1,out,,append
 /nopr

 *set,_ti_ang
 _ti_ang=nint(_rphst(_i_1)*10)/10

 /title,Design: %mname%  / Mechanical Angle: %_ti_ang%
 /gopr
 *if,_plt_opt(1),ne,0,then
  *if,b_mx,eq,0,then
    b_mx=2.8
  *endif
  ! the BSUM
  /cont,1,9,0,,b_mx
  *if,_plt_cur,eq,1,then
    cmsel,u,s_coil
  *endif

  _valmxx=b_mx
  _plvmxn=1
  esel,u,mat,,1,6,5
  nsle
  /num,1
  plns,b,sum  !  plnln
  esel,all
  nsel,all
  /uis,msgpop,3
  /out,rotsol1,out,,append
  esel,u,mat,,1,6,5
  nsle
  /graph,full
  nsort,b,sum,,,1
  *get,valmx_x,sort,,max
  *msg,info,cycle,valmx_x
  Cycle Number: %i  Maximum B magnitude:  %g
  *if,valmx_x,gt,_anl_mx(1),then
   _anl_mx(1)=valmx_x
  *endif
 *endif

 *if,_plt_opt(2),ne,0,then
  !  the HSUM
  *if,h_mx,eq,0,then
    h_mx=850000
  *endif
  /cont,1,9,0,,h_mx
  *if,_plt_cur,eq,1,then
    cmsel,u,s_coil
  *endif
  _valmxx=h_mx
  _plvmxn=1
  /num,1
  plns,h,sum  !  plnln,2
  /uis,msgpop,3
  /out,rotsol1,out,,append
  *msg,info,cycle,valmx_x
  Cycle Number: %i  Maximum H magnitude:  %g
  *if,valmx_x,gt,_anl_mx(2),then
   _anl_mx(2)=valmx_x
  *endif
 *endif

 *if,_plt_opt(3),ne,0,then
  !  the Relative Permeability
  etab,mu,nmisc,1
  *get,_unit,active,,solu,emunit      ! 1=mks, 2=cgs, 3=user
  _muz=1.2566e-6
  *if,_unit,eq,2,then
    _muz=1
  *endif
  sadd,mu,mu,,1/_muz
  *if,mu_mx,eq,0,then
    mu_mx=14500
  *endif
  /cont,1,9,0,,mu_mx
  esort,etab,mu,,,1
  *get,valmxx,sort,,max
  *msg,info,cycle,valmxx
  Cycle Number: %i  Maximum MU:  %g
  *if,valmxx,gt,_anl_mx(3),then
   _anl_mx(3)=valmxx
  *endif
  esel,u,mat,,1,6,5
  *if,_plt_cur,eq,1,then
    cmsel,u,s_coil
  *endif
  _valmxx=mu_mx
  _plvmxn=1
  /num,1
  plet,mur,1
  /uis,msgpop,3
  /out,rotsol1,out,,append
 *endif


 *if,_plt_opt(4),ne,0,then
  !  the stored energy
  *if,se_mx,eq,0,then
  se_mx=1
  *endif
  senergy,s_ce
  alls
  ! etab,mg_eng,sene
  ! ssum
  ! *get,valmxx,ssum,,item,mg_eng
  valmxx=s_eng
  *if,valmxx,gt,_anl_mx(4),then
   _anl_mx(4)=valmxx
  *endif
  /cont,1,9,0,,se_mx
  *if,_plt_cur,eq,1,then
    cmsel,u,s_coil
  *endif
  _valmxx=se_mx
  _plvmxn=1
  plet,mg_eng
  /uis,msgpop,3
  /out,rotsol1,out,,append
 *endif


 *if,_plt_opt(5),ne,0,then
  !  the energy error norm
  emagerr
  esort,etab,bn_err,,,1
  *get,valmxx,sort,,max
  *if,valmxx,gt,_anl_mx(5),then
   _anl_mx(5)=valmxx
  *endif
  *if,ber_mx,eq,0,then
    ber_mx=1
  *endif
  /cont,1,9,0,,ber_mx
  *if,_plt_cur,eq,1,then
    cmsel,u,s_coil
  *endif
  _valmxx=ber_mx
  _plvmxn=1
  plet,bn_err
  /uis,msgpop,3
  /out,rotsol1,out,,append
*endif

 *if,_plt_opt(6),ne,0,then
  ! the flux line plot
  alls
  plf2_d,27
  /uis,msgpop,3
  /out,rotsol1,out,,append
 *endif

 *if,_plt_opt(7),ne,0,then
  ! the Br line plot in the air gap
  alls
  /view
  /auto
  /yrang,-1.2,1.2
  /axlab,y,B-radial (T) in Air Gap
  /axlab,x,Arclength in Air Gap (m) from +X Axis
  /grid,1
  /xrange
  /num,2
  plpa,br
  /dist,,_g_dis
  /focus,1,_pfx,_pfy
  /user
  /uis,msgpop,3
  /yrange
 *endif

 *msg,info,_i_1
 Before the last enddo: cycle: %i
 chk_run
 *if,proceed,eq,0,exit
*enddo
*if,proceed,eq,0,:end

b_mx=_anl_mx(1)
h_mx=_anl_mx(2)
mu_mx=_anl_mx(3)
se_mx=_anl_mx(4)
berr_mx=_anl_mx(5)

*get,_rcpu2,active,,time,cpu
_del1=_rcpu2-_rcpu1

/auto
/view
/axlab,x,Mechanical Angle (D)
/axlab,y,Torque (N-M)
/grid,1
/num,2
_max_d=arg2
_min_a=arg1
_del_a=_max_d-_min_a
/title, Design: %mname% / Torque (MXWF) vs. Angle
*vscfun,zz,max,_rotm(1,1)
*vscfun,zz1,min,_rotm(1,1)

/yran,zz1,zz*1.05
! *vplo,_rotm(1,0),_rotm(1,1)   ! plots the Maxwell's stress torque results !
/yran
zz=  $  zz1=
!  compute the RMS
!*voper,_rot_m(1,2),_rot_m(1,1),mult,_rot_m(1,1)
!*voper,_rot_m(1,3),_rot_m(1,2),int1,_rot_m(1,0)
!_rms_t=sqrt(_rot_m(_max_d,3)/_del_a)
!  compute the torque

/gopr
!  load the data from the array _bh into the FITEM
_inumpt=_numcp
*get,_dim,parm,_rotm,dim,x
*set,_sene
*dim,_sene,,_dim,2
_dimrotm=_dim

*vfun,_sene(1,1),copy,_rotm(1,0)      !   12/2/96   
*vfun,_sene(1,2),copy,_rotm(1,1)      !   12/2/96   
_inumpt=_dim
*stat,_sene
fini
/prep7
csys
cm,_c_k,kp
ksel,all
lsel,none
*get,_kpmx,kp,,num,max
_kpmx=_kpmx+1
k,_kpmx,_sene(1,1),_sene(1,2)
k,_kpmx+1,_sene(_inumpt,1),_sene(_inumpt,2)

flst,2,2,3
fitem,2,_kpmx
fitem,2,_kpmx+1

flst,3,_inumpt-2,8

*do,_i1,2,_inumpt-1
 fitem,3,_sene(_i1,1),_sene(_i1,2),0
*enddo

bsplin,P51X,P51X

*get,bh_spl,line,,num,max
 _arg2=bh_spl    !   line number
 *get,_mxh,line,_arg2,leng

 _inumpt=51  !   number of points 
  _c_fract=-1/(_inumpt-1)
 *set,_sene1
 *dim,_sene1,table,_inumpt  !  2  ! 9/17

 *get,_mxh,line,_arg2,leng
 *do,_i2,1,_inumpt
  _c_fract=_c_fract+1/(_inumpt-1)
  _sene11=lx(_arg2,_c_fract)
  _sene10=ly(_arg2,_c_fract)

  _sene1(_i2,0)=_sene11
  _sene1(_i2,1)=_sene10
 *enddo
  /xran,arg1,arg2
  /yran
  /axlab,y,Torque (oz-in)
  /axlab,x,Mechanical Deg.
  _arg3=arg3
  /title,%mname%
  /grid,1
   *vplo,_sene1(1,0),_sene1(1,1)

! 1111111111111111111

   *vscfun,_cogtmn,min,_rotm(1,1)
   *vscfun,_cogtmx,max,_rotm(1,1)


/title, Design: %mname%


*if,_t_wnd,gt,0,then
 /com, compute the EMF from the data stored in coillink table
 /gopr
 *get,_dim,parm,coil_lnk,dim,x
 *set,_coiltab
 *dim,_coiltab,,_dim,2
 !  load the data from the array _bh into the FITEM
 cmsel,,stator
 csys,1
 *get,_thmx,node,,mxloc,y
 *get,_thmn,node,,mnloc,y
 _deltaa=_thmx-_thmn
 perfac=nint(360/_deltaa)
 _inumpt=_numcp

 *vfun,_coiltab(1,1),copy,coil_lnk(1,0)      !   12/2/96   
 *vfun,_coiltab(1,2),copy,coil_lnk(1,1)      !   12/2/96   
 _inumpt=_dim
 *vscfun,_maxang,max,_coiltab(1,1)

 *stat,_coiltab
 fini
 /prep7
 csys
 cm,_c_k,kp
 ksel,all
 lsel,none
 *get,_kpmx,kp,,num,max
 _kpmx=_kpmx+1
 k,_kpmx,_coiltab(1,1),_coiltab(1,2)
 k,_kpmx+1,_coiltab(_inumpt,1),_coiltab(_inumpt,2)

 flst,2,2,3
 fitem,2,_kpmx
 fitem,2,_kpmx+1

 flst,3,_inumpt-2,8

 *do,_i1,2,_inumpt-1
  fitem,3,_coiltab(_i1,1),_coiltab(_i1,2),0
 *enddo

 bsplin,P51X,P51X

 *get,cl_spl,line,,num,max
 *get,_mxh,line,cl_spl,leng

 _inumpt=51  !   number of points 
  _c_fract=-1/(_inumpt-1)
 *set,_coit1
 *dim,_coit1,table,_inumpt,2
 _coit1(0,0)=1e-6
 _coit1(0,1)=1
 _coit1(0,2)=2

 *get,_mxh,line,cl_spl,leng
 *do,_i2,1,_inumpt
  _c_fract=_c_fract+1/(_inumpt-1)
  _coit11=lx(cl_spl,_c_fract)
  _coit10=ly(cl_spl,_c_fract)
  _ex_t=0
  *if,_coit11,gt,_maxang,then
    _mxlen=_i2
    _ex_t=1
  *endif
  _coit1(_i2,0)=_coit11
  _coit1(_i2,1)=_coit10
  *msg,info,_i2,_coit11,_coit10,_c_fract
  _i2,_coit11,_coit10:  %i  %g  %g  _c_fract: %g
  *if,_ex_t,eq,1,exit
 *enddo
  parsave,all,temp1,par
 !ldel,cl_spl,cl_spl,1,1
 *if,_ex_t,eq,1,then
  *set,_t_arr
  *dim,_t_arr,table,_mxlen
  *vlen,_mxlen
  *vfunc,_t_arr(1,0),copy,_coit1(1,0)
  *vlen,_mxlen
  *vfunc,_t_arr(1,1),copy,_coit1(1,1)
  *set,_coit1
  *dim,_coit1,table,_mxlen,2
  _coit1(0,0)=1e-6
  _coit1(0,1)=1
  _coit1(0,2)=2
  *vfunc,_coit1(1,0),copy,_t_arr(1,0)
  *vfunc,_coit1(1,1),copy,_t_arr(1,1)
 *endif
 _angfac=3.141593/180
 _angfac1=(npole/2)/_angfac          !   4/15/01
 *voper,_coit1(1,0),_coit1(1,0),mult,_angfac
 *voper,_coit1(1,2),_coit1(1,1),der1,_coit1(1,0)
 emffac=-2*3.141593*_hfreq*perfac
 *voper,_coit1(1,2),_coit1(1,2),mult,emffac
 *voper,_coit1(1,0),_coit1(1,0),mult,_angfac1
 *vscfun,_emfmx,max,_coit1(1,2)
 *vscfun,_emfmn,min,_coit1(1,2)

 /com, compute the location of the first zero point
 *get,_dcoit,parm,_coit1,dim,x
 *set,_coit2
 *dim,_coit2,,_dcoit,2
 *vfunc,_coit2(1,1),copy,_coit1(1,0)
 *vfunc,_coit2(1,2),copy,_coit1(1,2)
 _int_s=0

 *do,_ic1,2,_dcoit
   *if,_coit2(_ic1,2)*_coit2(_ic1-1,2),le,0,then
     _dth_1=_coit2(_ic1,1)-_coit2(_ic1-1,1)
     _d_emf=_coit2(_ic1,2)-_coit2(_ic1-1,2)
     _m_th_em=_d_emf/_dth_1
     thth_0=_coit2(_ic1-1,1)-_coit2(_ic1-1,2)/_m_th_em
     _int_s=1
   *endif
   *if,_int_s,eq,1,exit
 *enddo

 /com, end of emf
 /xran,arg1*(npole/2),arg2*(npole/2)
 /axlab,y,EMF (V)
 /axlab,x,Electrical Deg.
 /title, %mname%  EMF for Phase A
 *vplo,_coit1(1,0),_coit1(1,2)
 /xran
*endif

fini
/post1
tcurr=0
*set,_real
esel,,mat,,6
*get,_elmn,elem,,num,min
*get,_real,elem,_elmn,attr,real
esel,r,real,,_real
_mg1=1
/nopr
curr2d
/out,rotsol1,out,,append
esel,all

/sho,xxx
/sho,term
/yran
/axlab,y,Torque (oz-in)
/axlab,x,Mechanical Deg.
/xran,arg1,arg2
/grid,1
*vplo,_sene1(1,0),_sene1(1,1)

/xran,arg1*(npole/2),arg2*(npole/2)
/axlab,y,EMF (V)
/axlab,x,Electrical Deg.
/title, %mname%  EMF for Phase A
*vplo,_coit1(1,0),_coit1(1,2)
/xran



/nopr
/out,torqmach,sum
*msg,info
>____SUMMARY OF MAXIMUMS/RMS FOR THE SOLUTIONS__<
*msg,info,arg1 
 Starting Angle (D):_____________________________ %g
*msg,info,arg2
 Ending Angle (D):_______________________________ %g
*msg,info,arg3
 Increment Angle (D):____________________________ %g
*msg,info,_me_elo
 Location of Zero Elec. Ang. (+CCW,+X axis):_____ %g
*if,tcurr,ne,0,then
 *msg,info
 NOTE: A current was applied to some part of the coil-This will&
 affect the torque calculation.
*endif
!*if,arg9,eq,0,then
! *msg,info
! Energy Term Used:_______________________________ Stored Energy
!*else
! *msg,info
! Energy Term Used:_______________________________ CO-Energy
!*endif

*if,cw_mot,eq,0,then
 *msg,info
 Rotation of the Rotor:__________________________ CCW
*else
 *msg,info
 Rotation of the Rotor:__________________________ CW
*endif
*msg,info,_cogtmn
 MIN torque (oz-in):_____________________________ %g
*msg,info,_cogtmx
 MAX torque (oz-in):_____________________________ %g
*msg,info,abs(_cogtmn-_cogtmx)
 Peak - Peak  torque (oz-in):____________________ %g
*msg,info,_i_1
 Number of solutions:____________________________ %g
*if,_plt_opt(1),eq,1,then
 *msg,info,_anl_mx(1)
 Maximum B magnitude:____________________________ %g
*endif
*if,_plt_opt(2),eq,1,then
 *msg,info,_anl_mx(2)
 Maximum H magnitude:____________________________ %g
*endif
*if,_plt_opt(3),eq,1,then
 *msg,info,_anl_mx(3)
 Maximum Permeability:___________________________ %g
*endif
*if,_plt_opt(4),eq,1,then
 *msg,info,_anl_mx(4)
 Maximum Stored Energy:__________________________ %g
*endif
*if,_plt_opt(5),eq,1,then
 *msg,info,_anl_mx(5)
 Maximum Normal B Error:_________________________ %g
*endif
*msg,info,_comtorq
 Component used for torque calculation:__________ %c
*if,tormeth,le,1,then
 *msg,info
  Method to compute torque:_____  Maxwells-line integral
*elseif,tormeth,eq,2,then
   *msg,info
  Method to compute torque:_____  Maxwells-torqsum
*else
 *msg,info
  Method to compute torque:_____  Virtual Work
*endif
*msg,info,_del1
 Time for all solutions (CPU):___________________ %g
*msg,info

*msg,info
__________SUMMARY OF ANGLES/Torque__________________
*msg,info
___________I___________Mechanical_____Torque________
*msg,info
________________________ANGLE (D)_____(Oz-in)_______
*vlen,_dimrotm
! *vwrite,sequ,_sene1(1,0),_sene1   
*vwrite,sequ,_rotm(1,0),_rotm(1,1)
(f15.0,f18.2,f12.2)
*msg,info
____________________________________________________

*if,_t_wnd,gt,0,then
/out,open_emf,sum
*msg,info
_________SUMMARY OF EMF FOR THE SOLUTIONS___________
*msg,info,arg1 
 Starting Mech.Angle (D):________________________ %g
*msg,info,arg2
 Ending Mech.Angle (D):__________________________ %g
*msg,info,arg3
 Increment Angle (D):____________________________ %g
*msg,info,_me_elo
 Location of Zero Elec.Ang.(+CCW,+X axis):_______ %g
*msg,info,_hfreq*60
 Rotor speed:____________________________________ %g (RPM)
*if,_int_s,eq,1,then
 *msg,info,nint(thth_0*10)/10
 Electrical angle of first zero point in EMF:____ %g (E)
*else
 *msg,info
 Electrical angle of first zero point in EMF:____ No zero point found
*endif
*if,tcurr,ne,0,then
 *msg,info
 NOTE: A current was applied to some part of the coil-This will&
 affect the EMF calculation.
*endif
*if,cw_mot,eq,0,then
 *msg,info
 Rotation of the Rotor:___________________ CCW
*else
 *msg,info
 Rotation of the Rotor:__ ________________ CW
*endif
*msg,info,nint(_emfmn*100)/100
 MIN EMF(V):______________________________ %g
*msg,info,nint(_emfmx*100)/100
 MAX EMF(V):______________________________ %g
*msg,info,nint(abs(_emfmn-_emfmx)*100)/100
 Peak-Peak EMF(V):________________________ %g
*msg,info,_i_1
 Number of solutions:____________________________ %g
*msg,info,nint(_del1)
 Time for all solutions (CPU):___________________ %g
*msg,info

*msg,info
__________SUMMARY OF ANGLES/EMF__________________
*msg,info
___________I___________Electrical________EMF________
*msg,info
________________________ANGLE (D)________(V)________
*vlen,_inumpt
! *vwrite,sequ,_sene1(1,0),_sene1
*vwrite,sequ,_coit1(1,0),_coit1(1,2)
(f15.0,f18.2,f12.2)
*msg,info
____________________________________________________
*endif

/out
/nopr
/out,tor_2d_%mname%,log
*msg,info,mname
 Model name:_______ %c

*msg,info
______________Torque (oz-in)_____________________
*msg,info
__Angle___rotor_____rotor_____stator_____stator____rotor_____stator
*msg,info
__________MXWF_______VW________MXWF________VW_______LI_________LI__
*vwrite,m_rotm(1,0),m_rotm(1,1),m_rotm(1,2),m_rotm(1,3),m_rotm(1,4),m_rotm(1,5),m_rotm(1,6)
(f7.3,6f10.1)
*msg,info
_________________________________________________
*msg,info

*stat
mpli
/out,rotsol1,out,,append
/gopr

*get,_mnu,active,,menu
*if,_mnu,gt,0,then
 *uili,torqmach,sum
 *if,_t_wnd,gt,0,then
   *uili,open_emf,sum
 *endif
*else
 *list,torqmach,sum
*endif


*endif

! $$$$$$$$$$$$$$$$$$$


parsav,all,temp,par
:end

/nopr
_nomsg=0    !  message/prompt display
_mg1=0
/cont
_plvmxn=0   !  do not override the limits
 *set,_ti_ang
/uis,msgpop,2
 _off_on=0
/gopr
/sho,term
_mg1=0



/out