/out,rotsols,out
/nerr,0,1e4
/com, rotsols.mac  04/8/02   !  support for 6.0
/com, rotsols.mac  11/24/01  !  using mvrot3ds.mac 
/com, rotsols.mac  11/02/01  !  torque log/torque option calculation
/com, rotsols.mac  10/10/01  !  if no 36 & current>0 sets current to 0
/com, rotsols.mac    9/9/01  !  deletes 36 temporarily if amp input=0
/com, rotsols.mac   4/14/01
!/uis,msgpop,3
/uis,defa
/gopr
!   rotsols.mac
!   Rotates the 3D machine , with no load or with a current form to the
!   coils, solves and computes the torque and generates a plot for each rotor position
!
!   The model is connected together  with CE's
!   The starting,ending, increment angles are specified
!   in the input
!  
!   The plots are stored in the file "mname".f33.  The parameter MNAME must be set
!   before the macro is executed.
!
!   The solution can be stopped by resetting the parameter PROCEED in the 
!   macro chk_run.mac.  If this is set in macro chk_run, then the user
!   must reset it after the run is stopped, else if the rotsols macro is reused, the
!   solution will be stopped in the first iteration of the new run, since it
!   was not reset to allow the runs to continue.  In restarting the runs, the existing 
!   files for the forces for the rotor and the stator will be appended in the
!   restart condition.
!
!
!     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).  This value is adjusted to correspond to an
!            integral number of steps.
!     arg4 = element component name for the calculation of the torque
!            (must be enclosed in single quotes), defaults to 'r_iron'
!     arg5 = name of stator file for stator forces.defaults to stator.dat
!     arg6 = name of rotor file for stator forces.defaults to rotor.dat
!     arg7 = peak current which is applied to the current form
!     arg8 = starting electrical angle for the starting mechanical angle
!     arg9 = 1,2   for plot option
!     ar10 = 0, inputs are by arguements
!          = 1, prompts the user for the input.
!     ar11 = 0, no action
!          NE0  frequency for EMF calculation
!     ar12 = 0 torque on rotor by maxwells stress
!          = 1 torque on rotor by virtual work
!          = 2 torque on stator by maxwells stress
!          = 3 torque on stator by virtual work
!
!   Parameters Required at the time the macro is called:
!            mname = name of db containing the unscaled, unmerged model
!
!         Definition of Plot Options for arg9
!            1 = BSUM
!            2 = HSUM
!
!   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.
!
!   At the end of this macro, the above parameters are set
!   to values obtained in these solutions
!
!   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
!
!   Summary file output:
!       tor_3d.sum      Summarizes the solutions, torque.
!       cog.sum         Running summary of the progress of the solutions
        tor_3d"mname"
!
!   array output:
!    _rotm(I,0) = index of angle (mechanical)
!    _rotm(I,1) = torque for each position
!
!    macros called:
!     mvrot3d  moves / connects the rotor to the stator
!     mvrot3ds  moves / connects the rotor to the stator for periodic skewed models
!     mvpsol   solves
!     tor_3d   computed 3d torque, defines components for g_force
!     g_force  writes out forces to the rotor and stator files
!     chk_run  used to stop solutions
!



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

/gopr
_half=.5*(_thmn+_thmx)
_def1=0
_arg_1=0
_def2=0
_arg_2=_half
_def3=0
_arg_3=0
_def4=0
_arg_4=0
_arg_5=1
_def5=0
_arg_9=0

*if,ar10,eq,1,then
 _arg_4=0
 _arg_1=0
 *ask,_arg_1,Starting Mechanical Angle,_def1
 *ask,_arg_2,Ending Mechanical Angle,_half
 *ask,_arg_3,Incremental Mechanical Angle,_def3     !   3/5/97
  !*ask,_arg_4,Torque component name ,_def4
 *ask,_arg_5,stator force file name,0
 *ask,_arg_6,rotor force file name,0
 *ask,_arg_7,peak Amperage,1
 *if,_arg_7,gt,0,then
   *ask,_arg_8,Starting Electrical Angle.,0
 *endif
 *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=_arg_6
 arg7=_arg_7
 arg8=_arg_8
 arg9=1
*else
 go_now=1
*endif

*msg,info,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10
args: %g   %g   %g   %g   %g   %g   %g   %g   %g   %g

*if,go_now,eq,0,:end

_arg5=arg5
_arg6=arg6
*get,_targ5,parm,_arg5,type
*if,_targ5,ne,3,then
  arg5=_stator
*endif

*get,_targ6,parm,_arg6,type
*if,_targ6,ne,3,then
  arg6=_rotor
*endif

!     ar12 = 0 torque on rotor by maxwells stress
!          = 1 torque on rotor by virtual work
!          = 2 torque on stator by maxwells stress
!          = 3 torque on stator by virtual work
!
!   tormeth=1      ! = 0 torque by Maxwell , =1, torque by Virtual Work
!   tor_loc=0      ! = 0 torque on rotor , =1, torque on stator
!

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

_mrknod=
*get,_mrknod,node,mrk_nod,nsel
*if,_mrknod,eq,0,then
 /nerr
 /out
 *msg,error
 The "Marker" node is not defined. This is the node used to determine&
 the orientation of the rotor. Without this node number, the macro cannot&
 be ran.  The parameter MRK_NOD must be the node number which defines the&
 zero mechanical position of the rotor. Set MRK_NOD to be this node number.
 /out,rotsols,out,,append
 /nerr,0,1e5
 _err=1
*endif
*if,_err,gt,0,:end


/com, check if 36s are present   10/13/01
esel,,ename,,36
*get,_e36,elem,,count
*if,_e36,le,0,then
  arg7=0
*endif

_s_36=0
*if,arg7,eq,0,then
  arg7=0
   *if,_e36,gt,0,then
    _s_36=1
    save,_temp36,db
    fini
    /prep7
    nsle
    edel,all
    ndel,all
    fini
    /solu
   *endif
*endif
_pk_amp=arg7

mvrs=0   !  default is to use mvrot3d    !  11/24/01
*if,skew_ang,gt,0,then                   !  11/24/01
  *if,nsp,ne,nspgen,then                 !  11/24/01
     mvrs=1                              !  11/24/01
  *endif                                 !  11/24/01
*endif                                   !  11/24/01



ar11=0
*if,ar11,le,0,then
  *get,_thfreq,parm,hfreq,type
  *if,_thfreq,lt,0,then
    ar11=60
  *endif
*endif
_hfreq=ar11

/out,g_force,out
/com,_________ force summary generations___________
/out,rotsols,out,,append

cmsel,,rotor
esel,r,mat,,2,3
cm,r_iron,elem
cycle=0
*get,_wall,active,,time,wall
/nopr
/out,rotsols,log
*msg,info
__RUNNING SUMMARY OF SOLUTIONS IN ROTSOLS.MAC____
*msg,info

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

/out,tor_3d%mname%,log
*msg,info
__RUNNING SUMMARY OF TORQUES IN ROTSOLS.MAC____
*msg,info

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

/out,rotsols,out,,append
/gopr


/com, start the coil summaries

/out,ld_coil,log
____RUNNING SUMMARY OF CURRENTS IN ROTSOLS.MAC____
*msg,info

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

 *if,_pk_amp,le,0,then
  *msg,info
  The peak current was entered as zero. The coils are unchanged.
  rlis
  /out,rotsols,out,,append
 *endif
/out,rotsols,out,,append
/gopr
_t_wnd=0
*get,_t_wnd,parm,wnd_,type
*vscfun,_nwmin,min,wnd_(1,3)
_t_wnd=_t_wnd*_nwmin


!*if,_pk_amp,gt,0,then
  _t_wnd=0  !  disable EMF calculation
!*endif

fini
/solu
alls

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

_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,rotsols,out,,append


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

/com, compute increments
*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
*endif                             !  3/5/97  
*stat,_numcp
*stat,_delth

_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

!        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
!
*set,m_rotm
*dim,m_rotm,table,_numcp,4
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

*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
esel,,mat,,5
nsle
csys,1
*get,_thmn,node,,mnloc,y
*get,_thmx,node,,mxloc,y
_full=0
*if,_thmx-_thmn,gt,180.0,then
  _full=1
*endif

fini
/solu
alls

_mg1=1

fmagbx,'s_iron'
/out,rotsols,out,,append

fmagbx,'r_iron'
/out,rotsols,out,,append


*if,_nosolve,eq,0,then

! save,%mname%,db
/com, move rotor to initial position
*if,_full,eq,1,then
  /auto
  csys,1
  *get,_mrk_rot,node,mrk_nod,loc,y
  _mv_zero=arg1-_mrk_rot-_delth
  *if,mvrs,eq,0,then         ! 11/24/01
   mvrot3d,_mv_zero,1        ! 10/22/96
  *else                      ! 11/24/01
   mvrot3ds,_mv_zero,1       ! 11/24/01
  *endif                     ! 11/24/01
  /out,rotsols,out,,append
  dsys,1
  nsel,all
  nlist,mrk_nod
  dsys

 *else
  *if,mvrs,eq,0,then            ! 11/24/01
   mvrot3d,_rphst(1)-_delth,1
  *else                         ! 11/24/01
   mvrot3ds,_rphst(1)-_delth,1  ! 11/24/01
  *endif                        ! 11/24/01
  /out,rotsols,out,,append
*endif
  /pbf,js,1
  eplo
  /user
/pbf,js
/sho,%mname%,f33

 _c_elc=arg8-npole/2*_delth
*stat,_c_elc
*stat,_numcp

*do,_i_1,1,_numcp
 /gopr
 cycle=cycle+1
 *msg,info,cycle,_i_1
  Solution No:_____ %i  _i_1: %i

 fini
 /prep7
 *if,cw_mot,ne,0,then
  /com,  clockwise motion of the rotor
  _meang=_me_elo-_rphst(_i_1)
 *else
  !  cw_mot = 0
   /com,  COUNTERclockwise motion of the rotor
  _meang=_me_elo+_rphst(_i_1)
 *endif

 !  move the rotor
 *if,_full,eq,1,then
  *if,mvrs,eq,0,then        ! 11/24/01
    mvrot3d,_delth,1
  *else                      ! 11/24/01
   mvrot3ds,_delth,1       ! 11/24/01
  *endif                     ! 11/24/01
  /out,rotsols,out,,append
 *else
  *if,mvrs,eq,0,then         ! 11/24/01
   mvrot3d,_meang,1
  *else                      ! 11/24/01
   mvrot3ds,_meang,1       ! 11/24/01
  *endif                     ! 11/24/01
   /out,rotsols,out,,append
 *endif
 csys,1
 *get,_c_theta,node,mrk_nod,loc,y
 csys
 !parsav,all,temp,par        ! 10/22/96
 !stitch,mname,_meang        ! 10/22/96
 !parres,,temp,par           ! 10/22/96

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

 /com, apply currents
 *if,_pk_amp,gt,0,then
  _c_elc=_c_elc+npole/2*_delth
  ld_coil,_c_elc,_pk_amp
  /out,ld_coil,log,,append
  *msg,info,_meang
  Mechanical angle:__________ %g
  *msg,info,_c_elc
  Electrical angle:__________ %g
  *list,ld_coil,sum
  rlis
  /out,rotsols,out,,append
 *endif


 /com,solve
 mvpsol
 /out,rotsols,out,,append
 fini
 /post1

 /com, compute torque
 tor_3d,2

 /out,tor_3d%mname%,log,,append
 *list,tor_3d,sum

 /out,rotsols,out,,append
 *list,tor_3d,sum


!     ar12 = 0 torque on rotor by maxwells stress    11/02/01 start
!          = 1 torque on rotor by virtual work
!          = 2 torque on stator by maxwells stress
!          = 3 torque on stator by virtual work
 m_rotm(_i_1,0)=_meang   !  _angrot
*if,ar12,eq,0,then
 _torcal=tormrot
*elseif,ar12,eq,1,
 _torcal=tor_rot
*elseif,ar12,eq,2
 _torcal=tormsta
*elseif,ar12,eq,3
  _torcal=tor_sta
*endif                                           !   11/02/01 end

m_rotm(_i_1,1)=tormrot*141.8
m_rotm(_i_1,2)=tor_rot*141.8
m_rotm(_i_1,3)=tormsta*141.8
m_rotm(_i_1,4)=tor_sta*141.8

/com, write forces to files
g_force,arg5,arg6,_meang,cycle,_numcp
/out,rotsols,out,,append

*get,_wall,active,,time,wall
/nopr
/out,rotsols,log,,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,rotsols,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

 alls
 /nopr
 /out,_data
 *set,_ti_ang
 *dim,_ti_ang,,1
 *vlen,1
 *vwrite,_rphst(_i_1)
 (f10.1)

 /out,rotsols,out,,append
 *vread,_ti_ang(1),_data
 (f10.1)
 /title,Design: %mname%  / Mechanical Angle: %_ti_ang(1)%
 /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,rotsols,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,rotsols,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,rotsols,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,rotsols,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,rotsols,out,,append
*endif

 *if,_plt_opt(6),ne,0,then
  ! the flux line plot
  alls
  plf2_d,27
  /uis,msgpop,3
  /out,rotsols,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


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
*if,proceed,eq,0,then
  _dim=_i_1
  _numcp=_i_1
*endif
*set,_sene
*dim,_sene,,_dim,2

*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)

/com, compute the RMS torque   !   4/14/01
*vscf,_max_a,max,_rotm(1,0)
*vscf,_min_a,min,_rotm(1,0)
_del_a=_max_a-_min_a
*vfun,_rot_m(1,1),copy,_rotm(1,1)
*voper,_rot_m(1,2),_rot_m(1,1),int1,_rot_m(1,0)
rms_tor=nint(_rot_m(_max_a,2)/_del_a)


/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=perfac/_angfac
 *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, end of emf
 /xran,arg1*perfac,arg2*perfac
 /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
esel,,mat,,6
*get,_elmn,elem,,num,min
*get,_real,elem,_elmn,attr,real
esel,r,real,,_real
_mg1=1
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)

*if,_t_wnd,gt,0,then
 /xran,arg1*perfac,arg2*perfac
 /axlab,y,EMF (V)
 /axlab,x,Electrical Deg.
 /title, %mname%  EMF for Phase A
 *vplo,_coit1(1,0),_coit1(1,2)
*endif

/xran



/nopr

/out,tor_3d%mname%,log,,append
*msg,info

*msg,info
______________Torque (oz-in)_____________________
*msg,info
__Angle___rotor_____rotor_____stator_____stator__
*msg,info
__________MXWF_______VW________MXWF________VW____
*vwrite,m_rotm(1,0),m_rotm(1,1),m_rotm(1,2),m_rotm(1,3),m_rotm(1,4)
(f7.3,4f10.1)
*msg,info

/out

/out,torqmach,sum
*msg,info
>____SUMMARY OF MAXIMUMS/RMS FOR THE SOLUTIONS__<
*msg,info,arg1 
 Starting Mechanical Angle (D)___________________ %g
*msg,info,arg2
 Ending Angle (D):_______________________________ %g
*msg,info,arg3
 Increment Angle (D):____________________________ %g
*if,_pk_amp,gt,0,then
*msg,info,arg8 
 Starting Electrical Angle (D)___________________ %g
*msg,info,currform
 Current form____________________________________ %c
*msg,info,_me_elo
 Location of Zero Elec.Ang.(+CCW,+X axis):_______ %g
*msg,info,_pk_amp
 peak current for winding (A):___________________ %g
*else
*msg,info
 Note:  The coils currents were not changed during these solutions
*endif
 *msg,info,arg5
 File containing the STATOR forces:______________ %c .dat
 *msg,info,arg6
 File containing the ROTOR forces:_______________ %c .dat
 *msg,info,mname
 File containing the plots:______________________ %c .f33
*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,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,rms_tor
 Average 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
!        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
*if,ar12,eq,0,then
 *msg,info
  Method to compute torque:_____ Rotor-Maxwell stress
*elseif,ar12,eq,1,then
   *msg,info
  Method to compute torque:_____ Rotor-Virtual work
*elseif,ar12,eq,2
 *msg,info
  Method to compute torque:_____ Stator-Maxwell stress
*else
 *msg,info
  Method to compute torque:_____ Stator-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,_numcp
! *vwrite,sequ,_sene1(1,0),_sene1
*vwrite,sequ,_rotm(1,0),_rotm(1,1)
(f15.0,f18.2,f12.1)
*msg,info
____________________________________________________

*if,_t_wnd,gt,0,then
/out,open_emf,sum
*msg,info
_________SUMMARY OF EMF 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
*msg,info,_hfreq
 Rotor speed:____________________________________ %g (Hz)
*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,_emfmn
 MIN EMF (V):_____________________________ %g
*msg,info,_emfmx
 MAX EMF (V):_____________________________ %g
*msg,info,abs(_emfmn-_emfmx)
 Peak - Peak  EMF(V):_____________________ %g
*msg,info,_i_1
 Number of solutions:____________________________ %g
*msg,info,_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.5)
*msg,info
____________________________________________________
*endif

/out

*if,_nomsg2,eq,0,then
 *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


*endif

/out,tor_3d%mname%,log,,append
*msg,info
________________________________________________

*msg,info

*list,torqmach,sum
*msg,info

*stat

mpli,3
/out,rotsols,out,,append

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


parsav,all,temp,par

*if,_s_36,eq,1,then
  resum,_temp36,db
  parres,,temp,par
*endif

: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